简体   繁体   English

Vaildate动态添加的数组输入-jQuery验证

[英]Vaildate dynamic added array inputs - jQuery Validate

Good Afternoon, 下午好,

Dynamic Fields are being added with jQuery and i am using jQuery Validate plugin to validate form. 动态字段与jQuery一起添加,我正在使用jQuery Validate插件来验证表单。

Code to create dynamic fields depend on number of selected options from selectbox. 创建动态字段的代码取决于selectbox中所选选项的数量。

$('#ids_noct').on('change', function() {
  var tsd = ( this.value );
  for(var j=1; j <= tsd; j++) {
     $("#t").append("<input name="name_ct[]" id="id_ct' + j + '" type="text" 
   class="form-control" placeholder="Add Variant' + j + ' Name" >");
  }
});

jQuery code to validate: jQuery代码进行验证:

$("#formsx").validate({
  rules: {
    "name_ct[]": {required: true}
  }

But with this only First field is getting validated not all. 但是,只有这一个“第一”字段才能得到验证。

Suppose if i have added 5 fields then 1 out of 5 is validates, not all. 假设如果我添加了5个字段,则5个中的1个是有效的,不是全部。

Also, i want to know that can i upload image using PHP and jQuery Validate. 另外,我想知道我可以使用PHP和jQuery Validate上传图像。 }); });

You have a couple basic problems in your code. 您的代码中有几个基本问​​题。 First, your append call doesn't have it's quotes set correctly - you can't start with a double quote and then include double quotes inside of it, it will throw an error. 首先,您的append调用未正确设置引号-您不能以双引号开头,然后在其中加双引号,这将引发错误。 So, your call should look more like this: 因此,您的呼叫应更像这样:

$("#t").append('<input name="name_ct[]" id="id_ct' + j + '" type="text" class="form-control" placeholder="Add Variant' + j + ' Name" >');

Secondly, you can't have inputs of type text with the same name attribute. 其次,您不能输入具有相同名称属性的文本类型。 So if you have 5 of these name_ct inputs, they each need to have their own name: 因此,如果您有5个name_ct输入,则每个输入都需要具有自己的名称:

$("#t").append('<input name="name_ct'+j+'" id="id_ct' + j + '" type="text" class="form-control" placeholder="Add Variant' + j + ' Name" >');

Finally, when you add a new field after $.validate has been called, you must also update the rules for it: 最后,在调用$.validate之后添加新字段时,还必须更新该规则:

$('#ids_noct').on('change', function() {
  var tsd = ( this.value );
  for(var j=1; j <= tsd; j++) {
     $("#t").append('<input name="name_ct'+j+'" id="id_ct' + j + '" type="text" class="form-control" placeholder="Add Variant' + j + ' Name" >');
     $('#id_ct'+j).rules('add',{
       required:true
     });
  }
});

Not knowing what exactly you want to do with these name_ct values, I can't say how you should recombine them, but in the submitHandler of $.validate, you can change the data however you need before doing a $.post . 不知道您到底要使用这些name_ct值做什么,我不能说您应该如何重新组合它们,但是在$ submitHandler中,可以在执行$.post之前更改所需的数据。

See it working here: http://jsfiddle.net/ryleyb/6syqa60d/ 看到它在这里工作: http : //jsfiddle.net/ryleyb/6syqa60d/

Please find related example solution below: 请在下面找到相关的示例解决方案:

HTML: HTML:

<form name="signupForm" class="cmxform" id="signupForm" method="get" action="">
<select name="category[]" id="cat_1">
<option value="">Select One</option>
<option value="1">aa</option>
<option value="2">bb</option>
<option value="3">cc</option>
<option value="4">dd</option>
</select>

<select name="category[]" id="cat_2">
<option value="">Select One</option>
<option value="5">ee</option>
<option value="6">ff</option>
<option value="7">gg</option>
<option value="8">hh</option>
</select>

<select name="category[]" id="cat_3">
<option value="">Select One</option>
<option value="9">ii</option>
<option value="10">jj</option>
<option value="11">kk</option>
<option value="12">ll</option>
</select>

<input class="submit" type="submit" value="Submit">
</form>

Now we will use jquery validation plugin jquery.validate.js for validating the form. 现在,我们将使用jquery验证插件jquery.validate.js来验证表单。 The condition will be that user will have to choose category from each dropdown. 条件是用户必须从每个下拉菜单中选择类别。 The script for validation will be as below: 验证脚本如下:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript">
$().ready(function() {
// validate the comment form when it is submitted
$("#signupForm").validate({
rules: {
"category[]": "required"
},
messages: {
"category[]": "Please select category",
}
});
});
</script>

Now the problem is that the readymade jquery.validate.js only validates the first element of category[]. 现在的问题是,现成的jquery.validate.js仅验证category []的第一个元素。 So, we need to modify it a little bit. 因此,我们需要对其进行一些修改。

In jquery.validate.js, we can find a function named checkForm, we have to modify it as below: 在jquery.validate.js中,我们可以找到一个名为checkForm的函数,我们必须对其进行如下修改:

checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
}

Try to use 尝试使用

$.validator.addClassRules("customer", { cRequired: true });

from documentation http://jqueryvalidation.org/reference 来自文档http://jqueryvalidation.org/reference

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

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