javascript - Field only required if certain dropdown is selected in a HTML form -


i wondering how field required if dropdown selected. in form, person select "canada" , 2 fields underneath appear(province , postal code), required before person submits form. , if person selected "united states", state , zip code appear instead.

the trouble having if choose either country, fields not associated country still required though hidden. need if choose united states, province nor postal code required, , same other way around.

any appreciated:) here form

<script type='text/javascript'>//<![cdata[   $('select[name=country]').change(function () {         if ($(this).val() == 'canada') {             $('#provinceselect').show();             $('#province').prop('required',true);          } else {             $('#provinceselect').prop('required',false);             $('#provinceselect').hide();             $('#province').prop('required',false);          }      });        $('select[name=country]').change(function () {         if ($(this).val() == 'canada') {             $('#postalselect').show();             $('#postal').prop('required',true);          } else {             $('#postalselect').prop('required',false);             $('#postalselect').hide();             $('#postal').prop('required',false);          }      });           </script>  <!--html form-->  <form action="pppaymentform/pppaymentform.php"  method="post" name="topchoiceform" id="topchoiceform">         <input placeholder="company name" type="text" name="companyname">         <input placeholder="first name" type="text" name="first_name" required>         <input placeholder="last name" type="text" name="last_name" required>         <input placeholder="email" type="email" name="email" id="email" required>      <!--country select-->          <select class="countrycss" id="country" name="country" required>     <option value="" selected="selected">please select country</option>     <option value="canada" id="canada">canada</option>     <option value="united states" id="united states">united states</option>      </select>      <!--province select-->     <div id="provinceselect" style="display:none;">     <select class="provincecss" id="province" name="province" required>     <option value="" selected="selected">please select province</option>     <option value="alberta" id="alberta">alberta</option>     <option value="british coloumbia" id="british coloumbia">british coloumbia</option>      </select>     </div>      <!--state select-->     <div id="stateselect" style="display:none;">     <select class="statecss" id="state" name="state">     <option value="" selected="selected">please select state</option>     <option value="arkansas" id="arkansas">alaska</option>     <option value="hawalli" id="hawalli">hawalli</option>      </select>      </div>            <!--postal select-->         <div id="postalselect" style="display:none;">         <input placeholder="postal code" type="text" name="postal" required>         </div>           <!--zip select-->         <div id="zipselect" style="display:none;">         <input placeholder="zipcode" type="text" name="zip" required>         </div>          <input placeholder="city" type="text" name="city" required>          <input placeholder="address" type="text" name="address1" required>          <input name="shipping" class="shipping" type="radio" id="shipping" checked/>         <label class="shippingcheck">$19.99 shipping – database sent on cd rom </label>         <br>         <input name="shipping" class="shipping" type="radio" id="noshipping" />         <label class="shippingcheck">free shipping – database sent via email</label>          <br>         <button name="submit" type="submit" id="submit">pay now</button> 

you use .prop() method, each of .change have like

$('select[name=country]').change(function () {         if ($(this).val() == 'canada') {             $('#provinceselect').show();             $('#provinceselect').prop('required',true);         } else {             $('#provinceselect').prop('required',false);             $('#provinceselect').hide();         }     }); 

i'm curious why have 4 different instances of .change() have put them under same instance of change() since reacting same thing.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -