Sample code for storing checkbox values in jquery array

  • 2020-03-30 00:59:21
  • OfStack

 
<input type="checkbox" id="checkAll" value="1"> Future generations / All don't choose  
<input type="checkbox" name="items" value="1">1 
<input type="checkbox" name="items" value="2">2 
<input type="checkbox" name="items" value="3">3 
<input type="checkbox" name="items" value="4">4 
<input type="checkbox" name="items" value="5"> 
<input type="button" onclick="show()" value=" Prompt selected "> 
<script> 
$("#chekcAll").click(function(){ 
if(this.checked){ 
$("input[name=items]").attr("checked","checked"); 
} 
else{ 
$("input[name=items]").attr("checked",null); 
} 
}) 
function show(){ 
var strIds=new Array();//Declare an array of ids
$("input[name=items]").each(function (i,d){ 
if (d.checked) { 
strIds.push(d.value); 
} 
}) 
if(strIds.length<1) 
alert(" You did not select an item !"); 
else{ 
var ids=strIds.join(","); 
alert(" The string you selected is: "+ids); 
} 


} 

</script> 

Related articles: