Home > Article > Web Front-end > How to get multiple values selected in checkbox in js
Idea: Use the name attribute value to obtain the checkbox object, and then loop to determine the checked attribute (true means selected, false means unchecked). Here is an example demonstration:
1, HTML structure
<input type="checkbox" name="test" value="1" /> <span> 1 </span> <input type="checkbox" name="test" value="2" /> <span> 2 </span> <input type="checkbox" name="test" value="3" /> <span> 3 </span> <input type="checkbox" name="test" value="4" /> <span> 4 </span> <input type="checkbox" name="test" value="5" /> <span> 5 </span> <input type='button' value='提交' onclick="show()" />
2, javascript code (jQuery)
function show(){ obj = document.getElementsByName("test"); check_val = []; for(k in obj){ if(obj[k].checked) check_val.push(obj[k].value); } alert(check_val); }
3. Demonstration effect
The above is the detailed content of How to get multiple values selected in checkbox in js. For more information, please follow other related articles on the PHP Chinese website!