简体   繁体   中英

if Html.CheckBox is checked or not

I have a simple question regarding 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

Instead of

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

You want

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

Useful howto blog here: Linky

Using jQuery you can check like this

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

For your Reference-jQuery Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM