简体   繁体   English

使用jQuery表单验证插件验证动态生成的字段(通过条件脚本)

[英]Validation of dynamically generated fields(through script on a condition) using jquery form validation plugin

I have a form where few fields are generated on the basis of selecting an option, ie, a dropdown which has number as value, on the basis of this number(being the count) few fields are generated and appended in loop. 我有一种形式,其中在选择一个选项的基础上生成的字段很少,即,一个具有数字作为值的下拉列表,在此数字(即为计数)的基础上,生成了几个字段并将其附加到循环中。 I've included validation of the other fields, but not for these generated fields. 我已经包括了其他字段的验证,但是没有针对这些生成的字段进行验证。

An example of my scenario, 我的情况的一个例子

$('select.select-dependent').change(function(){

    var sel_value = $('select.select-dependent').val();      

    if(sel_value==0)
    {   
        //Resetting Dependends Section      
        $("#dependent-show-area").empty();
        $("#dependent-show-area").css({'display':'none'});
    }
    else{

        //Resetting Field Section 
        $("#dependent-show-area").empty();

        //Below Function Creates Input Fields Dynamically 
        create(sel_value);
    }   

});



function create (sel_value) {
    for(var i=1;i<=sel_value;i++) {
                    html +=     '<div class="row">';    

        html +=     '<div class="col-xs-12 col-md-6">';
        html +=     '<label>Field 1 label</label>';
        html +=     '<input class="form-control" type="text" name="field1[]"/>';
        html +=     '</div>';

        html +=     '<div class="col-xs-12 col-md-6">';
        html +=     '<label>Field 2 label</label>';
        html +=     '<input class="form-control" type="text" name="field2[]"/>';
        html +=     '</div>';
        html +=     '</div>';

        html +=     '<div class="row">';
        html +=     '<div class="col-xs-12 col-md-6">';
        html +=     '<label>Field 3 label</label>';
        html +=     '<input class="form-control" type="text" name="field3[]"/>';
        html +=     '</div>';
        html +=     '<div class="col-xs-12 col-md-6">';
        html +=     '<label>Field 4 label</label>';
        html +=     '<input class="form-control" type="text" name="field4[]"/>';
        html +=     '</div>';
        html +=     '</div>';

        $("section#id-to-append").append(html);
    }
}

In the above code the create() function creates few fields and appends it to a section in my form, this is done in a loop. 在上面的代码中,create()函数创建了几个字段,并将其追加到表单中的部分中,这是在循环中完成的。 What I've been trying is to find is a way to validate these generated fields. 我一直在尝试找到一种验证这些生成字段的方法。

I've tried the below method to validate but it doesn't seem working 我尝试了以下方法进行验证,但似乎无法正常工作

   var settings = $("#my-form").validate().settings;
   $.extend(settings, {
             rules: {
                'field1[]': {
                    required: true
                },
                'field2[]': {
                    required: true
                },
                'field3[]': {
                    required: true
                },
                'field4[]': {
                    required: true
                }
            },
     });

Please note that I'm using jQuery Form Validation Plugin for this purpose and I''m looking for a way to accomplish a solution with the same. 请注意,我正在为此目的使用jQuery Form Validation Plugin ,并且我正在寻找一种使用该方法来完成解决方案的方法。 Any help will be much appreciated. 任何帮助都感激不尽。

If all the fields are going to have the same validation then you can try adding a common class name and use rules() function to add rules. 如果所有字段都将具有相同的验证,则可以尝试添加一个通用的类名,并使用rules()函数添加规则。

$( ".commonclass" ).rules( "add", {
    required: true
});

Also if you have doubt whether the rule is assigned or not, use the following code to find it out, 另外,如果您不确定规则是否已指定,请使用以下代码找出,

$('.commonclass').rules()

this function call will return an object of all assigned rules. 此函数调用将返回所有已分配规则的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM