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

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

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:

 &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;

 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:

  &lt;input bind=@colorSelected id=&quot;changeColor&quot; type=&quot;color&quot; value=&quot;#55BF3B&quot; @onchange=&quot;HandleColorChange&quot;&gt;

  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          
  }

答案1

得分: 0

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

这里是一个可工作的示例:

@for(int i=0; i &lt; item.Length; i++)
{
    int b = i;
    <div>
        <span>选择器 @i :</span>  <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B"  @onchange="(args =&gt; HandleColorChange(args, b))">
    </div>
}

@for(int i=0; i &lt; item.Length; i++)
{
    <div>
        <span>@i 的选择:</span> @item[i]
    </div>
}

@code
{  
    string colorSelected = "blue";

    string[] item = new string[10];

    void HandleColorChange(ChangeEventArgs args, int itemNo)
    {
        colorSelected = args.Value.ToString();         
        item[itemNo] = colorSelected;     
    }
}

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

英文:

Here you have working example:

@for(int i=0; i &lt; item.Length; i++)
{
    int b = i;
    &lt;div&gt;
        &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;
    &lt;/div&gt;
}

@for(int i=0; i &lt; item.Length; i++)
{
    &lt;div&gt;
        &lt;span&gt;Selected for @i :&lt;/span&gt; @item[i]
    &lt;/div&gt;
}

@code
 {  
     string colorSelected = &quot;blue&quot;;

     string [] item = new string[10];

    void HandleColorChange(ChangeEventArgs args, int itemNo)
    {
        colorSelected = args.Value.ToString();         
        item[itemNo] = colorSelected;     
    }
 }

https://blazorrepl.telerik.com/QxEhlQle42JJ49dz28

答案2

得分: 0

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

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

<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.

&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:

确定