英文:
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:
<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
}
答案1
得分: 0
以下是您的代码的中文翻译部分:
这里是一个可工作的示例:
@for(int i=0; i < item.Length; i++)
{
int b = i;
<div>
<span>选择器 @i :</span> <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B" @onchange="(args => HandleColorChange(args, b))">
</div>
}
@for(int i=0; i < 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 < item.Length; i++)
{
int b = i;
<div>
<span>Selector for @i :</span> <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B" @onchange="(args => HandleColorChange(args, b))">
</div>
}
@for(int i=0; i < item.Length; i++)
{
<div>
<span>Selected for @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;
}
}
答案2
得分: 0
如果您想使用@onchange
属性,则只需使用value
和@onchange
属性。
在您的代码中,您需要删除bind
属性,并将值设置为您的@colorSelected
字段。
<input id="changeColor" type="color" value="@colorSelected" @onchange="((ChangeEventArgs args) => HandleColorChange(args, itemNo))">
英文:
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.
<input id="changeColor" type="color" value="@colorSelected" @onchange="((ChangeEventArgs args) => HandleColorChange(args, itemNo))">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论