javascript - modifying a multiple selects validation with jquery -
what i've got far:jsfiddle
what want is:
- add class "invalid"
<select>
if not selected in it's row - remove class if 3 selects in row selected
- if
<select>
s in 1 row selected submit form - if 1
<select>
selected add "invalid" class other 2 selects in same row
this html , js included in fiddle link above :
<form id="productoptions" name="product-options"> <div class="selects s1"> <select name="selectss1" id="size1" class="product-select-options-size"> <option>-size</option> <option>small</option> <option>medium</option> <option>large</option> <option>x-large</option> </select> <select name="selectsc1" id="color1" class="product-select-options-color"> <option>-color</option> <option>green</option> <option>pink</option> <option>white</option> <option>yellow</option> </select> <select name="selectsq1" id="qty1" class="product-select-options-qty"> <option>-qty</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> </div> <div class="selects s2"> <select name="selectss2" id="size2" class="product-select-options-size"> <option>-size</option> <option>small</option> <option>medium</option> <option>large</option> <option>x-large</option> </select> <select name="selectsc2" id="color2" class="product-select-options-color"> <option>-color</option> <option>green</option> <option>pink</option> <option>white</option> <option>yellow</option> </select> <select name="selectsq2" id="qty2" class="product-select-options-qty"> <option>-qty</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> </div> <div class="selects s3"> <select name="selectss3" id="size3" class="product-select-options-size"> <option>-size</option> <option>small</option> <option>medium</option> <option>large</option> <option>x-large</option> </select> <select name="selectsc3" id="color3" class="product-select-options-color"> <option>-color</option> <option>green</option> <option>pink</option> <option>white</option> <option>yellow</option> </select> <select name="selectsq3" id="qty3" class="product-select-options-qty"> <option>-qty</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> </div> <p><a href="#" class="add-more">+ add more @ 1 time</a></p> <!-- end select options --> <input type="image" name="product-options" src="/media/blackheart/images/blackheart/add-to-cart.png" /> </form>
kindly advise
you posted obscure question. instance, did not on event "invalid" class has added/removed. suppose 'submit'.
also gave 4 conditions , #1, 2, 4 ones redundant cause mutually exclusive degree. i.e. if add class "invalid" if not selected in it's row sense of remove class if 3 selects in row selected ? have simply remove class if selected. again, #4 condition in essence same #1.
your js-code works well, must add several lines manipulate css-classes note: did not use jsfiddle, checked code in real browser (including ie7) , worked, whereas didn't @ jsfiddle.
$(function(){ $('#frm_productorder').on('submit', function(e){ e.preventdefault(); var f=$(this); var invalid=false, selected=0; $('.selects').each(function(){ var row=$(this); var items=0; $('select', row).each(function(){ var t=$(this); var selectedindex=t.prop("selectedindex"); if(selectedindex) { items++; t.removeclass('invalid'); //this line added } else t.addclass('invalid'); //this line added }); if(items==3) selected++; if(items>0 && items<3) invalid=true; items=0; }); if(selected>0 && !invalid) f[0].submit(); else alert('please fill form correctly'); }); });
Comments
Post a Comment