admin管理员组文章数量:1287628
Hello I am building a form with 1 checkbox. The rest of the form fields work fine but whether the checkbox is checked or not the value is always 'on'.
My code:
<input id="checkbox_<?=$row['csid'];?>" type="checkbox" <?php if($row['feedbackVisible'] == 'yes') { echo "checked='true'";}?> >
and how i get value with jQuery:
var review_visible_website = $('#checkbox_'+ modalId).val();
The checkbox starts checked but if i uncheck it, it still gives me value 'on'. What am I doing wrong?
Hello I am building a form with 1 checkbox. The rest of the form fields work fine but whether the checkbox is checked or not the value is always 'on'.
My code:
<input id="checkbox_<?=$row['csid'];?>" type="checkbox" <?php if($row['feedbackVisible'] == 'yes') { echo "checked='true'";}?> >
and how i get value with jQuery:
var review_visible_website = $('#checkbox_'+ modalId).val();
The checkbox starts checked but if i uncheck it, it still gives me value 'on'. What am I doing wrong?
Share Improve this question edited Jun 24, 2016 at 15:41 Charlie 23.9k12 gold badges63 silver badges95 bronze badges asked Jun 24, 2016 at 15:37 BRGBRG 3806 silver badges25 bronze badges 4- Can you see it as checked? – Mohit Bhardwaj Commented Jun 24, 2016 at 15:39
- i can see it as check but when i uncheck it it still gives me value as on – BRG Commented Jun 24, 2016 at 15:40
- What gives you the value as on? – j08691 Commented Jun 24, 2016 at 15:41
- 1 Possible duplicate of How do I check if a checkbox is checked? – Hanlet Escaño Commented Jun 24, 2016 at 15:49
2 Answers
Reset to default 8Here are few ways you can test a check box with JQuery
// First method - Remended
$('#checkbox').prop('checked') // Boolean true
// Second method - Makes code more readable (e.g. in if statements)
$('#checkbox').is(':checked') // Boolean true
// Third method - Selecting the checkbox & filtering by :checked selector
$('#checkbox:checked').length // Integer >0
$('#checkbox:checked').size() // .size() can be used instead of .length
// Fourth method - Getting DOM object reference
$('#checkbox').get(0).checked // Boolean true
$('#checkbox')[0].checked // Boolean true (same as above)
Another way using javascript you can do like this:
if( document.getElementById('checkbox').checked == true ){
// checked
}
else{
// not checked
}
本文标签: javascriptCheckbox is always checked whatever I chooseStack Overflow
版权声明:本文标题:javascript - Checkbox is always checked whatever I choose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304901a2371308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论