admin管理员组文章数量:1432196
I want to check in javascript whether a checkbox in bootstrap-multiselect has been checked. Usually checkboxes are created using the label and the type="checkbox" attribute. In that case I can use
document.getElementById(id).checked
to test whether the checkbox is checked or not. But in bootstrap-multiselect, the label is used. Here is my html code:
<select class="demo" multiple="multiple">
<option id="test_id" value="test_value">test_value</option>
</select>
If I use the function document.getElementById(id).checked, I get an "undefined" instead of true or false? What is the correct way to test for a checked or unchecked box in this case? thanks carl
I want to check in javascript whether a checkbox in bootstrap-multiselect has been checked. Usually checkboxes are created using the label and the type="checkbox" attribute. In that case I can use
document.getElementById(id).checked
to test whether the checkbox is checked or not. But in bootstrap-multiselect, the label is used. Here is my html code:
<select class="demo" multiple="multiple">
<option id="test_id" value="test_value">test_value</option>
</select>
If I use the function document.getElementById(id).checked, I get an "undefined" instead of true or false? What is the correct way to test for a checked or unchecked box in this case? thanks carl
Share Improve this question asked Aug 14, 2015 at 17:53 carlcarl 4,43611 gold badges60 silver badges110 bronze badges 2-
id
in an option...that's new – Hackerman Commented Aug 14, 2015 at 18:08 -
Have you tried
document.getElementById(id).selected
? – Fabio Commented Aug 14, 2015 at 18:21
3 Answers
Reset to default 4Just push all the selected values into an array and manipulate the info as needed.
HTML
<select class="demo" multiple="multiple" >
<option id="test_id1" value="test_value1">test_value1</option>
<option id="test_id2" value="test_value2">test_value2</option>
<option id="test_id3" value="test_value3">test_value3</option>
<option id="test_id4" value="test_value4">test_value4</option>
</select>
<div id='selectedVals'></div>
jQuery
$(".demo").change(function() {
var allSelected = new Array();
$(".demo option:selected").each(function(){
allSelected.push(this.value);
});
$('#selectedVals').html(allSelected)
});
https://jsfiddle/wa4mjmaj/
Here's a function that returns whether a specific option is checked using its id:
function isChecked(optionId) {
return $(".demo > #" + optionId).get(0).selected;
}
Here's a fiddle using JQuery. I assume you're using JQuery if you're using Bootstrap. Those are select options you have, not checkboxes, btw ;)
https://jsfiddle/d1L8uott/
Code:
<select class="demo" multiple="multiple" id="mySelect">
<option id="test_id1" value="test_value">test_value1</option>
<option id="test_id2" value="test_value">test_value2</option>
<option id="test_id3" value="test_value">test_value3</option>
</select>
$('#mySelect').change(function(){
alert($('#mySelect option:selected').text());
});
本文标签: javascriptcheck whether a checkbox in bootstrapmultiselect has been checkedStack Overflow
版权声明:本文标题:javascript - check whether a checkbox in bootstrap-multiselect has been checked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584460a2664814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论