Make Jquery validation plugin work on inputs with no name attribute

空扰寡人 提交于 2019-12-04 04:04:45

What should I do for the new inputs added dynamically?

Give them a name, any name

Is there a way to use the plugin without using the name attr?

No

Is there another solution?

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?

correction - need unique names see comments

var i = 0;
$("#add-choice").live("click",function(){
 $("#choices").append("<input type='text' name='bob" + i + "' class='required'/><br>");
 i = i + 1;
});

Yes you can do it, but with limitations

Limitation: You will not have flexibility to change messages while using different rules.

<form ="validationForm">
<input type="text" class="required" title="required message" />
</form>

jQuery("#validationForm").validate();

Quote OP:

"Is there a way to use the plugin without using the name attr"

No, you cannot use the jQuery Validate plugin if you do not have name attributes on all of your fields.

As per documentation:

Markup recommendations

The name attribute is '''required''' for input elements, the validation plugin doesn't work without it.

The "workaround" is to have jQuery create a unique name when it creates the new input.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!