C# 根据值选中或取消选中复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11839525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:27:18  来源:igfitidea点击:

Make a checkbox checked or unchecked based on the value?

c#asp.net-mvc

提问by always v

How can we make a checkbox checked or unchecked programatically based on the value? That is to say, for a particular user if the value is true, the checkbox should be checked, else if the value is false the checkbox needs to be unchecked. I declared the checkbox in the following way:

我们如何根据值以编程方式选中或取消选中复选框?也就是说,对于特定用户,如果值为true,则应选中复选框,否则,如果值为false,则需要取消选中复选框。我通过以下方式声明了复选框:

<input type="checkbox" class="checkbox">

采纳答案by Yasser Shaikh

if(condition = true)
{
@Html.CheckBoxFor(x => x.Test, new { @checked = "checked" })
}
else
{
@Html.CheckBoxFor(x => x.Test)
}

Hope this helps :)

希望这可以帮助 :)

回答by hYk

You have to have a name for the checkbox, eg:

您必须为复选框命名,例如:

<input name=checkBox1 type="checkbox" class="checkbox">

For functionality :

对于功能:

    if(x==1){
   checkBox1.Checked = true; //To Check
}
else{
   checkBox1.Checked = false // To Uncheck
}

回答by Umesh

If you want to check/uncheck check box value in code-behind, you have to include an ID and runat server attributes in your check box tag.

如果要在代码隐藏中选中/取消选中复选框值,则必须在复选框标记中包含 ID 和 runat 服务器属性。

<checkbox Id="chk" runat="server" class="chkbox"/>

code-behind:

代码隐藏:

if(yourcondition==true)
  chk.checked = true;
else
  chk.checked = false;

If you want to do it in javascript

如果你想用javascript来做

<checkbox Id="chk" class="chkbox"/>

JS:

JS:

if(yourcondition==true)
  chk.checked = true;
else
  chk.checked = false;

回答by Faust

If you're using MVC and passing in model values correctly from your controller, then

如果您使用 MVC 并从控制器正确传递模型值,那么

@Html.CheckBoxFor(model => model.checkBox1)

...is all you need. The html-helper does the logic to figure out whether or not to insert the checked="checkbox"code.

...是你所需要的全部。html-helper 执行逻辑以确定是否插入checked="checkbox"代码。

Otherwise, without the HTML-helper you can dynamically generate the attribute yourself (others have pointed out how), but don't make the mistake of thinking that checked = ""will leave the box unchecked. See this answerfor an explanation.

否则,如果没有 HTML 助手,您可以自己动态生成属性(其他人已经指出了如何),但不要错误地认为checked = ""会取消选中该框。请参阅 此答案以获取解释。

回答by DavGarcia

There is a simpler way to include or not include the checkedattribute if you are writing our your own <input>instead of using alternatives such as Html.CheckBoxFor:

checked如果您正在编写自己的属性<input>而不是使用替代方法,则有一种更简单的方法来包含或不包含该属性,例如Html.CheckBoxFor

<input type="checkbox" checked="@isChecked">

Razor is smart enough to automatically generate either

Razor 足够智能,可以自动生成

<input type="checkbox" checked="checked">

or

或者

<input type="checkbox">

depending on whether isCheckedis trueor false. No need for if statements or duplicate code.

取决于isCheckedtrue还是false。不需要 if 语句或重复代码。

回答by ranah

If you do not want to use @Html.CheckBoxFor for whatever reason and you'd like to stick to

如果您出于任何原因不想使用 @Html.CheckBoxFor 并且想坚持使用

         <input type="checkbox"> 

then this is what I found to be the best way to do it:

那么这就是我发现的最好的方法:

 <input @(Convert.ToBoolean(Model.YourPropertyHere) == true ?   "checked='checked'" : string.Empty)  type="checkbox" />

The code that @Yasser provided above:

@Yasser 上面提供的代码:

    checked="@(required ? "checked" : "")"

Did not work for me because it was still adding the checked attribute to the element, and setting checked="" will still render the check box as checked, which was not the desired output, instead if you wrap the whole statement into a razor block like so:

对我不起作用,因为它仍在向元素添加 checked 属性,并且设置 checked="" 仍会将复选框呈现为选中状态,这不是所需的输出,而是将整个语句包装到一个剃刀块中像这样:

     @(Convert.ToBoolean(Model.YourPropertyHere) == true ?   "checked='checked'" : string.Empty)

you will get the desired results.

你会得到想要的结果。

回答by Raj

try this:

尝试这个:

<input type="radio" name="filter_favorites" value="Favorites" @(loggedIn ? "checked" : "") />

So this is a simple radio button which checks loggedInvariable & if true radio button will be checked else not.

所以这是一个简单的单选按钮,它检查loggedIn变量,如果真单选按钮将被检查,否则不检查。

回答by Prince

if(condition = true)
{
@Html.CheckBoxFor(x => x.Test, new { @checked = true })
}
else
{
@Html.CheckBoxFor(x => x.Test)
}