在 <select>
中进行选择后,我需要能够运行函数.问题是我也绑定(bind)了@bind,当我尝试使用@onchange 时出现错误,指出它已被@bind 使用。我尝试使用@onselectionchange,但这什么也没做(不运行该功能)。我可能会忘记@bind,而只是将@onchange 分配给一个函数,但我不确定如何将选定的值传递给函数。
我有以下代码:
<select @bind="@SelectedCustID" @ @onchange="@CustChanged" class="form-control">
@foreach (KeyGuidPair i in CustList)
{
<option value="@i.Value">@i.Text</option>
}
</select>
谢谢。
最佳答案
@bind
本质上等同于同时拥有 value
和 @onchange
,例如:
<input @bind="CurrentValue" />
相当于:<input value="@CurrentValue" @onchange="@((ChangeEventArgs e) => CurrentValue = e.Value.ToString())" />
既然你已经定义了 @onchange
, 而不是添加 @bind
,只需添加 value
为防止冲突:<select value="@SelectedCustID" @onchange="@CustChanged" class="form-control">
@foreach (KeyGuidPair i in CustList)
{
<option value="@i.Value">@i.Text</option>
}
</select>
来源:https://docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-3.1
关于select - Blazor:在使用 @bind 时如何在 <select> 中使用 onchange 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125929/