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
Add a ment  | 

3 Answers 3

Reset to default 4

Just 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