Joke Collection Website - Public benefit messages - How to get all the values ??of checkbox

How to get all the values ??of checkbox

jquery’s traversal method can obtain the desired selected value of the check box

$("input: checkbox: checked").each(function(index, element)); / / Execute the function for all selected checkboxes. The value of each checkbox can be taken out from the function body

$("input:checkbox:checked").map(function(index,domElement)) ; // Generate new jQuery objects for all selected checkboxes through function return values

Example demonstration: click the button to get the selected value of the checkbox

Create Html element

lt;div class="box"gt;

lt;spangt;Click the button to get the selected value of the checkbox:lt;/spangt;

lt;div class=" content"gt;

lt; input type='checkbox' name='message' value='1'/gt; send text message

lt; input type='checkbox' name ='message' value='2'/gt;Send mail

lt;/divgt;

lt;input type="button" value="Submit"gt;

p>

lt;/divgt;

Set css style

div.box{width: 300px; padding: 20px; margin: 20px; border: 4px dashed #ccc; }

div.boxgt; span{color: #999; font-style: italic;}

div.content{width: 250px; margin: 10px 0; padding: 20px; border: 2px solid #ff6666;}

Write jquery code

$(function(){

$("input: button").click(function () {

text = $("input: checkbox[name='message']: checked").map(function(index,elem) {

return $(elem ).val();

}).get().join(', ');

alert("The value of the selected checkbox is: " text);

p>

});

});