如果选中Html.CheckBox

时间:2022-04-03 08:52:38

I have a simple question regarding html.checkbox

我有一个关于html.checkbox的简单问题

Using this as an example:

以此为例:

@Html.CheckBox("IncreaseStock", Model.IncreaseStock == null ? false : (bool)Model.IncreaseStock)

now doing a alert to see if the checkbox is tick or not:

现在做一个警报,看看复选框是否勾选:

var IncreaseStock = $('#IncreaseStock').val();

if (IncreaseStock == true) {
    alert("true ")
} else {
    alert("NOT TRUE")
}

However when its false it still shows as true, even though i didnt click the check box.

然而,当它的错误它仍然显示为真,即使我没有点击复选框。

Any ideas

2 个解决方案

#1


1  

Instead of

$('#IncreaseStock').val();

You want

$('#IncreaseStock').prop('checked')

Useful howto blog here: Linky

有用的howto博客:Linky

#2


1  

Using jQuery you can check like this

使用jQuery你可以像这样检查

if($('#IncreaseStock').is(':checked')){
   alert('true');
}
else{
 alert('false');
}

For your Reference-jQuery Docs

对于您的Reference-jQuery文档

#1


1  

Instead of

$('#IncreaseStock').val();

You want

$('#IncreaseStock').prop('checked')

Useful howto blog here: Linky

有用的howto博客:Linky

#2


1  

Using jQuery you can check like this

使用jQuery你可以像这样检查

if($('#IncreaseStock').is(':checked')){
   alert('true');
}
else{
 alert('false');
}

For your Reference-jQuery Docs

对于您的Reference-jQuery文档