使Jquery验证插件可在没有名称属性的输入上工作 [英] Make Jquery validation plugin work on inputs with no name attribute
问题描述
我花了很长时间才发现JQuery表单验证插件仅适用于具有名称"属性的字段.
Well it took me long enough to find out that the JQuery form validation plugin only works on fields with a "name" attribute.
无论如何,我用它来填写所有必填字段.
Anyway, I use it to make all the fields required.
我的问题是,在某个时候,我只有一个文字输入
My problem is that at some point, I have one text input
<div id='choices'>
<input type='text' />
</div>
<a href='#' id='add-choice' >Add input </a>
用户可以根据需要添加任意数量的文本输入:
the user can add as many text inputs as he wants :
$("#add-choice").live("click",function(){
$("#choices").append("<input type='text' /><br>");
});
我希望也需要这些新字段.即使我给第一个输入起了名字.对于动态添加的新输入,我应该怎么做?
And I want these new fields to be also required. Even if I give a name to the first input. What should I do for the new inputs added dynamically ?
是否可以使用不使用名称attr的插件,还是有另一种解决方案?
Is there a way to use the plugin without using the name attr, or is there another solution ?
推荐答案
对于动态添加的新输入,我该怎么办?
给他们一个名字,任何名字
Give them a name, any name
是否可以使用不使用名称attr的插件?
否
还有其他解决方案吗?
这取决于您对字段的处理方式-除了使jquery验证工作以外,您显然不需要其他任何名称,所以为什么不直接命名并全部命名呢?那?
It depends on what you are doing with the fields - you clearly don't need a name on them for any other reason than to make jquery validate work, so why don't you just make up a name and call them all that?
更正-需要唯一的名称,请参见注释
var i = 0;
$("#add-choice").live("click",function(){
$("#choices").append("<input type='text' name='bob" + i + "' class='required'/><br>");
i = i + 1;
});
这篇关于使Jquery验证插件可在没有名称属性的输入上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!