Check checkbox - at least one selected

I have a number of checkboxes and one more checkbox for "Select All"

I want to check if the user has selected at least one checkbox. Need a modification in javascript

<script language="Javascript"> function doSubmit(){ function check_checkboxes() { checked=false; var c = document.getElementsByTagName('INPUT'); for (var i = 1; i < c.length; i++) { if (c[i].type == 'checkbox') { if (c[i].checked) { return true} else {alert("Please identify what warehouses comply:"); } } } //if I place my struts action here..its not working? } document.holiDay.command.value= 'addingApp'; //My Struts action if something checked. document.holiDay.submit(); } 
+4
source share
3 answers
 var all=document.getElementById('holiDay'); 

The HTML ids must be unique, so getElementById returns only one element. Perhaps you can try getElementsByTagName - http://msdn.microsoft.com/en-us/library/ms536439(VS.85).aspx ?

Sort of...

 function check_checkboxes() { var c = document.getElementsByTagName('input'); for (var i = 0; i < c.length; i++) { if (c[i].type == 'checkbox') { if (c[i].checked) {return true} } } return false; } 

and change the Validate function to ...

 function Validate() { if(!check_checkboxes()) { alert("Please identify what warehouses comply:"); return false; } return true; } 
+4
source

Select at least one checkbox using jqQery. Try the following code.

 $('input[type="checkbox"][name="class"]').on('change', function () { var getArrVal = $('input[type="checkbox"][name="class"]:checked').map(function () { return this.value; }).toArray(); if (getArrVal.length) { //execute the code } else { $(this).prop("checked", true); alert("Select at least one column"); return false; } ; }); 
+3
source
  (function() { for(x in $ = document.getElementsByTagName("input")) with($[x]) return (type == "checkbox" ? checked == true : 0) }) 
0
source

Source: https://habr.com/ru/post/1315214/


All Articles