php - Disable a submit button when one of the fields is empty -
i trying disable submit button when one or more of selectors (generated dynamically) shown in picture empty..
i tried following jquery code:
<script> var $submit = $("input[name=store_values]"); $(".ownlevelselect").each(function(){ if($(".ownlevelselect:empty").length>0){ $submit.attr("disabled","disabled"); }else { $submit.removeattr("disabled"); } }); </script>
and these relevant parts of form:
echo "<select class='ownlevelselect' id='ownlevelselect' name='level-".$compi['competence_id']."' >";
and input buttons:
echo "<input type='submit' name='submit_values' value='save'>"; echo "<input type='submit' name='store_values' value='store'></form>";
the 1 want disable 1 name='store_values'
there's ambiguous in question, mean empty dom empty (selects contains no elements) or empty selects empty value selected ?
if mean empty emtpy value, can try this, handling change event shadow wizard said:
$('document').ready(function(){ var submitbutton = $('input[name="store_values"]'); /* called disable button on page load, done here possible server-side */ checkvalues() /* called on change event on dropdowns */ $('select.ownlevelselect').on('change',function(){ checkvalues(); }); function checkvalues(){ /* disable button if found empty <select> (mean, no <option> inside), or select empty value selected */ submitbutton.prop('disabled',$(".ownlevelselect > option:selected[value=''],.ownlevelselect:empty").length > 0); } });
if mean empty "dom empty" (no option defined inside select), can try (you can place function , call each time dynamically add element)
$('document').ready(function(){ var submitbutton = $('input[name="store_values"]'); submitbutton.prop('disabled',$(".ownlevelselect:empty").length > 0); });
edit
be carefull ids, don't have relevant part of code sure, seem reuse same id selects, bad idea :)
edit2
here's fiddle, can see if missed something: http://jsfiddle.net/d8rb9/
Comments
Post a Comment