I am trying to find out whenever a checkbox on my page is checked and unchecked. I console log to find out the status, but I am always getting an "unchecked" result. What am I doing wrong?
...
<input type="checkbox" id="checkbox_addAuthor" name="checkbox_addAuthor" />
</div>
</form>
@section Scripts
{
<script type="text/javascript">
var checkbox = $('#checkbox_addAuthor');
var authorList = $('#AuthorList');
checkbox.on('click',function(){
if(checkbox.Checked == true)
{
console.log("I am checked")
}
else {
console.log("I am not checked");
}
})
</script>
}
When I run this, the console always prints out "I am not checked". Cant figure out why.
$('#checkbox_addAuthor :checkbox:checked').length > 0;
you can use the prop
if($('#checkbox_addAuthor').prop('checked'))
{
// something when checked
}
else {
// something else when not
}
使用$('#checkbox_addAuthor').val() == "on"
来检查复选框的状态
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.