JQuery Validation for Array of Input Elements

前端 未结 4 611
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 06:50

I need to validate an array of input text elements (mileage): For example:


  
        

        
4条回答
  •  被撕碎了的回忆
    2020-12-09 07:27

    You have to loop through in these cases, like this:

    $(document).ready(function() {
        $("#form1").validate({
            submitHandler: function(form) {
                form.submit();
            }
        });  
        $("#form1 input[name='mileage']").each(function() {
           $(this).rules("add", { required: true });
        });  
    });
    

    .rules() only affects the first match, so you need a .each() on there to loop through and add any rules to all matches.

提交回复
热议问题