Jquery stop form submit unless checkbox selected -


i've got code, doesn't seem return true. alert shows up. thoughts (without needing create var in store checkbox has been selected seems bit hacky).

thanks.

$("#form").submit(function(e) {     $('input[type=checkbox]').each(function () {         if(this.checked){             return true;         }     });     alert("please select @ least 1 upgrade.");     return false; }); 

you don't need loop determine if checkbox checked. :checked selector filters out not checked. can use $.length count of checked inputs.

$("#form").submit(function(e) {     if(!$('input[type=checkbox]:checked').length) {         alert("please select @ least 1 upgrade.");          //stop form submitting         return false;     }      return true; }); 

additionally, using approach, small change should make work

$("#form").submit(function(e) {     $('input[type=checkbox]').each(function () {         if($(this).is(':checked')){             return true;         }     });     alert("please select @ least 1 upgrade.");     return false; }); 

i think may missing $() around this


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -