我试图在捕获选定值后取消元素的“onchange”事件。基本上只是为了捕获所选值并将选择元素所选项目返回到它所在的位置。我如何在 Blazor 中执行此操作?
<select @onchange="onSelectFilter">
<option value="0">Select a filter</option>
<option value="1">Filter 1</option>
<option value="2">Filter 2</option>
</select>
@code
{
private void onSelectFilter(ChangeEventArgs args)
{
var val = int.Parse(args.Value.ToString());
if (val != 0)
{
//do something
//reset the value to 0
args.Value = 0;
}
}
}
最佳答案
仅在 C# 中实现的更简单的方法
@page "/"
<select @bind="Selected" class="form-control">
<option value="0">Select a filter</option>
<option value="1">Filter 1</option>
<option value="2">Filter 2</option>
</select>
<h1>@Captured</h1>
@code
{
int Captured;
int _Selected;
private int Selected
{
get{return _Selected;}
set{
Captured = value; //store the captured value
_Selected = 0; //reset the drop down
}
}
}
您可以使用这个@code block 实现更小化
@code
{
int Captured;
int Selected
{
get{return 0;} // reset the select
set{Captured = value;} //store the captured value
}
}
关于c# - 取消事件 (onchange) 或在 Blazor 中检索旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193797/