开发者

jquery validate input but use id for identifying fields

开发者 https://www.devze.com 2023-03-30 05:29 出处:网络
I have fields in following format: <input type=\"text\" name=\"cities[]\" id=\"city_zip1\"/> <input type=\"text\" name=\"cities[]\" id=\"city_zip2\"/>

I have fields in following format:

<input type="text" name="cities[]" id="city_zip1"/>
<input type="text" name="cities[]" id="city_zip2"/>
<input type="text" name="cities[]" id="city_zip3"/>

Is there a way to make first one required by validate plugin, but to leave name as it is? Or to make one of th开发者_如何学Pythonese three required, but to leave name as it is.


Done on click for example. Can be done on submit.

$("#checkforvalue").click(function(e) {
    e.preventDefault();
    if ($("input#place_of_services:checked").val()) {
       var zipentered = false;
       $("input[id^='city_zip']").each(function() {
           if ($(this).val().length) {
               zipentered = true;
           }
       });

       if (zipentered) {
           alert("value in one of the fields");
       } else {
           alert("no value entered");
       }
   }
});

js fiddle example

0

精彩评论

暂无评论...
验证码 换一张
取 消