仍不清楚我怎么可以使用jQuery的validate()如何验证动态表单字段使用jQuery验证?
假设我有类似下面的表单验证动态表单字段:
<form id="myform" action="" ...>
<input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
....
<button type="button" class="clone btn btn-primary" value="Add">
....
<button type="submit" name="submit" value="submit">
...
</form>
正如你可以看到我有一个充满活力的领域(cauthor []);所以用户可以点击添加按钮来创建一个新的输入字段。然后点击添加按钮后,我的表单将如下所示:
<input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
<input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
<input type="text" form-control" name="cauthor[]" id="cauthor1" value=""/>
现在的问题是?如何验证所有的字段?
$("#casos_clinicos_form").validate({
rules: {
'cauthor[]': {
required: true
}
},
messages: {
'cauthor[]': "Author name not informed! Please correct and try again."
}
});
它只验证第一个。
如果您知道如何处理上述问题,请回复。
您可以为这些控件分配一个类,然后验证将起作用 –
@RashidJaved,绝对错误。这个插件要求所有输入节点都包含一个唯一的'name'。使用你的方法,只有第一个实例将被验证。 http://jsfiddle.net/mcqb2vk2/ – Sparky