如何在Blazor中在onchange事件中传递多个参数

huangapple go评论87阅读模式
英文:

How to pass multiple parameters on onchange in blazor

问题

以下是您要翻译的内容:

"Is it possible to pass multiple parameters to onchange event in blazor? If it is not possible, may I know what are my options. Thank you.

Requirement: User should have the ability to set color of their choice for each item. So, I need to pass additional parameter (itemNo) during each color selection.

Tried to pass extra parameter, but does not compile, throws error:

<input bind=@colorSelected id="changeColor" type="color" value="#55BF3B" @onchange="((ChangedEventArgs args) => HandleColorChange(args, itemNo))">

void HandleColorChange(ChangeEventArgs args, int itemNo)
{
colorSelected = args.Value.ToString();
//I need to set color as below for one specific item
item[itemNo] = colorSelected;
}

Current working Code, if no extra parameter passed:

<input bind=@colorSelected id="changeColor" type="color" value="#55BF3B" @onchange="HandleColorChange">

void HandleColorChange(ChangeEventArgs args)
{
colorSelected = args.Value.ToString();
//But I need to set this color for one specific item, for which I need to pass another parameter
}"

英文:

Is it possible to pass multiple parameters to onchange event in blazor?.
If it is not possible, may I know what are my options. Thank you.

Requirement: User should have the ability to set color of their choice for each item. So, I need to pass additional parameter (itemNo) during each color selection.

Tried to pass extra parameter, but does not compile, throws error:

  1. &lt;input bind=@colorSelected id=&quot;changeColor&quot; type=&quot;color&quot; value=&quot;#55BF3B&quot; @onchange=&quot;((ChangedEventArgs args) =&gt; HandleColorChange(args, itemNo))&quot;&gt;
  2. void HandleColorChange(ChangeEventArgs args, int itemNo)
  3. {
  4. colorSelected = args.Value.ToString();
  5. //I need to set color as below for one specific item
  6. item[itemNo] = colorSelected;
  7. }

Current working Code, if no extra parameter passed:

  1. &lt;input bind=@colorSelected id=&quot;changeColor&quot; type=&quot;color&quot; value=&quot;#55BF3B&quot; @onchange=&quot;HandleColorChange&quot;&gt;
  2. void HandleColorChange(ChangeEventArgs args)
  3. {
  4. colorSelected = args.Value.ToString();
  5. //But I need to set this color for one specific item, for which I need to pass another parameter
  6. }

答案1

得分: 0

以下是您的代码的中文翻译部分:

  1. 这里是一个可工作的示例:
  2. @for(int i=0; i &lt; item.Length; i++)
  3. {
  4. int b = i;
  5. <div>
  6. <span>选择器 @i :</span> <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B" @onchange="(args =&gt; HandleColorChange(args, b))">
  7. </div>
  8. }
  9. @for(int i=0; i &lt; item.Length; i++)
  10. {
  11. <div>
  12. <span>@i 的选择:</span> @item[i]
  13. </div>
  14. }
  15. @code
  16. {
  17. string colorSelected = "blue";
  18. string[] item = new string[10];
  19. void HandleColorChange(ChangeEventArgs args, int itemNo)
  20. {
  21. colorSelected = args.Value.ToString();
  22. item[itemNo] = colorSelected;
  23. }
  24. }

链接:https://blazorrepl.telerik.com/QxEhlQle42JJ49dz28

英文:

Here you have working example:

  1. @for(int i=0; i &lt; item.Length; i++)
  2. {
  3. int b = i;
  4. &lt;div&gt;
  5. &lt;span&gt;Selector for @i :&lt;/span&gt; &lt;input bind=@colorSelected id=&quot;changeColor&quot; type=&quot;color&quot; value=&quot;#55BF3B&quot; @onchange=&quot;(args =&gt; HandleColorChange(args, b))&quot;&gt;
  6. &lt;/div&gt;
  7. }
  8. @for(int i=0; i &lt; item.Length; i++)
  9. {
  10. &lt;div&gt;
  11. &lt;span&gt;Selected for @i :&lt;/span&gt; @item[i]
  12. &lt;/div&gt;
  13. }
  14. @code
  15. {
  16. string colorSelected = &quot;blue&quot;;
  17. string [] item = new string[10];
  18. void HandleColorChange(ChangeEventArgs args, int itemNo)
  19. {
  20. colorSelected = args.Value.ToString();
  21. item[itemNo] = colorSelected;
  22. }
  23. }

https://blazorrepl.telerik.com/QxEhlQle42JJ49dz28

答案2

得分: 0

如果您想使用@onchange属性,则只需使用value@onchange属性。

在您的代码中,您需要删除bind属性,并将值设置为您的@colorSelected字段。

  1. <input id="changeColor" type="color" value="@colorSelected" @onchange="((ChangeEventArgs args) => HandleColorChange(args, itemNo))">

Microsoft文档

英文:

If you want to use the @onchange attribute then you need to use only value and @onchange attributes.

In your code you need to remove the bind attribute and set the value to your @colorSelected field.

  1. &lt;input id=&quot;changeColor&quot; type=&quot;color&quot; value=&quot;@colorSelected&quot; @onchange=&quot;((ChangeEventArgs args) =&gt; HandleColorChange(args, itemNo))&quot;&gt;

Microsoft docs

huangapple
  • 本文由 发表于 2023年7月12日 22:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定