简体   繁体   English

如何使用jQuery Validate插件验证动态内容

[英]How to validate dynamic content using jQuery Validate plugin

I'm having issues validating some dynamic content. 我在验证一些动态内容时遇到问题。 The closest I have come to finding a similar problem is this: jquery-validate-is-not-working-with-dynamic-content 我最近发现相似问题的方法是: jquery-validate-is-not-with-dynamic-content

My form is a basic form, enter name, email, phone etc. But there's this question "How many passengers?" 我的表格是基本表格,输入姓名,电子邮件,电话等。但是这里有一个问题“有多少乘客?” This is a select and depending on how many passengers you select, I use jQuery to create more fields based on this amount using this: 这是一个select ,根据您选择的乘客人数,我使用jQuery根据此数量使用以下内容创建更多字段:

$('select.travellers').attr('name','Number of travelers').on('click', function() {
          var travellers = this.value; //On change, grab value
          var dom = "";
          for(var i = 0; i < travellers; i++){ //for 0 is less than travellers
              dom += '<label>Full Name</label>';
              dom += '<input type="text" name="FullName_'+i+'">';
              dom += '<label>Food&nbsp;requirements</label>';
              dom += '<select size= "0" name="Food Requiries_'+i+'"  tabindex="-1"  >
                      <option value="No pork">No pork</option>
                      <option value="Halal">Halal</option>
                      <option value="Food allergies">Food allergies</option>
                      <option value="Other">Other</option></select>';
          }
          $('.form_Additional').html(dom); //add dom into web page
        });

The output is an input field asking for the additional passenger name and a select asking for their food requirements 输出是一个input字段,询问其他乘客姓名,并select一个询问其食物需求

How do I validate these newly created elements? 如何验证这些新创建的元素? This is what I have so far: 这是我到目前为止的内容:

$(document).ready(function(e) {

    //validation rule for select
    $.validator.addMethod("valueNotEquals", function(value, element, arg){
      return arg != value;
     }, "Value must not equal arg.");


    //validate form
    $("#FORMOB7DC24203803DC2").on("click", function(event){
        $(this).validate({
            rules: {
                FullName:{
                    required: true  
                },
                FullName_0:{
                    required: true  
                },
                'Number of travelers':{
                    valueNotEquals: "Please select" 
                }
            }
        });
    });
});

Is there a way to make this dynamic? 有没有办法使这种动态? Because this form allows for up to 30 passengers and I don't want to manually write in rules FullName_0 , FullName_1 , FullName_2 etc etc. 由于此表单最多可容纳30位乘客,因此我不想手动输入规则FullName_0FullName_1FullName_2等。

I added the rule FullName_0 and it doesn't validate so I don't know what I'm doing wrong. 我添加了规则FullName_0 ,但该规则无效,因此我不知道自己在做什么错。

note - simplified code for readability 注意-简化代码以提高可读性

You can use attributes to add the rules like 您可以使用属性添加规则,例如

dom += '<input type="text" name="FullName_' + i + '" required>';

Demo: Fiddle 演示: 小提琴

You need to use 您需要使用

$(document).on('click', '#FORMOB7DC24203803DC2', function(e) {
    validate();
});

This allows you to validate content added to page at any time. 这使您可以随时验证添加到页面的内容。

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

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