如何为inputRadio添加一个on change事件?



有人知道如何在inputRadio中添加on change事件吗?

我知道你可以这样做

<input type="radio" @onchange="ChangeFunction"/>

这将击中ChangeFunction()但是如果我执行以下操作

<InputRadioGroup @bind-Value="@i.LeaveRequest.DayTime">
<label>Full Day</label>
<InputRadio Value="@FullDay" class="checkbox-P" @onchange="ChangeFunction"/>
</InputRadioGroup>

它永远不会到达函数

我也试过将@onchange="ChangeFunction"添加到InputRadioGroup行,但似乎都没有达到功能。

有人知道怎么做吗?

您可以创建普通的单选按钮:

@foreach (var item in new string[] {"AspNetCore","AspNet","SomeJsThingWhatever"})
{
<div>
<input type="radio" name="technology" id="@item" value="@item" 
@onchange="RadioSelection" 
checked=@(RadioValue.Equals(item,StringComparison.OrdinalIgnoreCase)) 
/>
<label for="@item">@item</label>
</div>
}
<div>
<label>Selected Value is @RadioValue</label>
</div>
@functions
{
string RadioValue = "aspnetcore";
void RadioSelection(ChangeEventArgs args)
{
RadioValue = args.Value.ToString();
}
}

来源

Blazor page

<EditForm Model="Items">
<InputRadioGroup ValueChanged="@((e) => OnRadiochange(e))" TValue="string" ValueExpression="() => SelectedValue">
@foreach (var item in Items)
{
bool Checked= false;
if (SelectedValue.Equals(item, StringComparison.OrdinalIgnoreCase))
{
Checked = true;
}
<div class="form-check">
<InputRadio Value="@item" class="form-check-input" checked=@Checked />
@item <br />
</div>
}
</InputRadioGroup>

背后的代码
public string SelectedValue { get; set; } = "AspNetCore";
public List<string> Items { get; set; } = new List<string> { "AspNetCore", "AspNet", "SomeJsThingWhatever" };
private void OnRadiochange(object sender)
{
SelectedValue = (string)sender;
StateHasChanged();
}

相关内容

  • 没有找到相关文章

最新更新