我正在尝试使用Blazor框架获取复选框的值,如果它被选中,但是到目前为止我还没有找到任何方法。当我将绑定放在复选框中时,它总是被选中。我无法弄清如何获取选中的值。
这是我的代码:
<input type="checkbox" id="addition" name="math" value="add" bind="@name" />
<label for="addition">Addition</label>
将复选框的值绑定到布尔值。
在您的@Code中,bool checkedValue = false; // 或true,根据您的用例
在您的HTML中:
<input type="checkbox" checked @bind="checkedValue">
checkedValue的值将与您的复选框的值相同。
@page "/registration"
@foreach (var club in ClubList())
{
<input type="checkbox" @onchange="eventArgs => { CheckboxClicked(club, eventArgs.Value); }" />@club<br />
}
@functions {
public List<string> ClubMember { get; set; } = new List<string>();
void CheckboxClicked(string clubID, object checkedValue)
{
if ((bool)checkedValue)
{
if (!ClubMember.Contains(clubID))
{
ClubMember.Add(clubID);
}
}
else
{
if (ClubMember.Contains(clubID))
{
ClubMember.Remove(clubID);
}
}
}
public List<String> ClubList()
{
// fetch from API or...
List<String> c = new List<String>();
c.Add("Clube01");
c.Add("Clube02");
return c;
}
}
checked="@(HasClubMember(clubID))"
。 - Slatec.Add("Clube01");
是一个笔误(应该是 Clube 而不是 Club),而 c.Add("Clube02");
则是复制粘贴时传播了这个错误 :) - MikeT我认为其他答案都没有解决我所寻找的两个问题:
我的解决方案实现了这两个功能,但需要重复使用<input>标签来区分选中和未选中的版本。
Razor文件如下:
@if (Approved)
{
<input type="checkbox" checked @onchange="@(async (e) =>
await ToggleApprovedAsync(false))" />
}
else
{
<input type="checkbox" @onchange="@(async (e) =>
await ToggleApprovedAsync(true))" />
}
@code {
bool Approved;
override async Task OnInitializedAsync()
{
Approved = await HttpClient.GetJsonAsync<bool>("../api/IsApproved");
}
async Task ToggleApprovedAsync(bool approved)
{
Console.WriteLine("Toggle Approved " + approved );
if (approved)
{
await HttpClient.PostJsonAsync<bool>($"../api/Approve", null);
Approved = true;
}
else
{
await HttpClient.PostJsonAsync<bool>($"../api/Disapprove", null);
Approved = false;
}
}
}
移除value属性:
<input type="checkbox" id="addition" name="math" bind="@name" />
public bool name {get;set;}
<input type="checkbox" @bind-value="YourConditionTypeOfBool" checked="@(YourConditionTypeOfBool?"checked":null)"/> Is True
<span>My condition is @(YourConditionTypeOfBool?"True":"False")</span>
@code{
bool YourConditionTypeOfBool = true;
}
你需要移除value="add"
部分。
并确保name
是布尔型。
编辑:完整示例
@page "/test2"
<input type="checkbox" bind="@boolvalue" /><br/>
Boolvalue: @boolvalue<br/>
<button onclick="@toggle">toggle</button>
@functions
{
public bool boolvalue { get; set; }
void toggle()
{
boolvalue = !boolvalue;
}
}
public bool name {get;set;}
然后您可以使用@bind-value属性:
<input type="checkbox" id="name" @bind-value=name class="form-check-input">
或者您可以使用内置的InputCheckbox组件:
<InputCheckbox @bind-Value="starship.IsValidatedDesign" />
忘记提到 InputCheckbox 必须放在 EditForm 内。我来到这里主要是想再次检查语法,因为我有点脑抽,而且这仍然是谷歌的首选结果。我的特定情况类似于Mike的情况,我需要复选框的更改事件来驱动另一个组件是否可见。
我的解决方案使用了Flores的建议,以某种方式整理了Mike的想法。这只是对话框的基本结构,使用了我的机制:
<div class="modal fade show" id="newsApprove" style="display:block;background-color:rgba(10,10,10,8);" aria-modal="true" role="dialog">
<div class="modal-dialog" style="vertical-align:central">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Dates Active</h4>
<button type="button" class="close" @onclick="@Cancel">×</button>
</div>
<div class="modal-body">
<div class="input-group mb-3">
<div class="input-group-text">
<input type="checkbox" aria-label="Active From Now?" id="activeNow" @bind-value="isChecked" />
</div>
<label class="form-check-label" for="activeNow">Render This Active Immediately?</label>
</div>
@if (!isChecked)
{
<div>
<SfDateTimePicker TValue="DateTime" Min="DateTime.Now" @bind-Value="activeFrom"></SfDateTimePicker>
</div>
}
<SfDateTimePicker TValue="DateTime" Min="DateTime.Now" @bind-Value="activeTo"></SfDateTimePicker>
</div>
<div class="modal-footer">
<button type="submit" class="btn-primary" @onclick="@Save">Approve</button>
<button type="button" class="btn-secondary" @onclick="@Cancel">Cancel</button>
</div>
</div>
</div>
</div>
@code {
public DateTime activeTo { get; set; }
public DateTime activeFrom { get; set; }
private bool isChecked { get; set; } = true;
}
从那里开始,如果您需要在复选框更改事件触发时进行更多处理,则可以添加一个方法来执行相关处理并相应地切换isChecked的值。
checked
parameter seems to be set by default. So in this case you can just delete thechecked
attribute. For unchecked checkbox use:<input type="checkbox" @bind="checkedValue" unchecked>
- ssamko