开发者

jquery validation plugin for newly created fields

开发者 https://www.devze.com 2022-12-28 16:58 出处:网络
I am using jquery validation plugin.开发者_StackOverflow社区 I have an \'add file field\' button which will add new file fields dynamically on click. I want to validate these fields on submit. How can

I am using jquery validation plugin.开发者_StackOverflow社区 I have an 'add file field' button which will add new file fields dynamically on click. I want to validate these fields on submit. How can I do this ? Please help..


You have to use jQuery's live or delegate methods http://api.jquery.com/live/ to bind the inputs created with a new validation rule.

E.g. (untested)

$('#addFieldButton').live('click', function() {
  $('#myFormId').append('<input type="text" id="newfield" name="newfield" />');
  $('#newfield').rules('add', {
    minlength: 2
  });
}


Never used the plugin, but I believe you need to apply the .validate(); method to any elements created after document/DOM load.

$newFileInput=$('<input type="file" name="files[]" />');
$newFileInput.appendTo($('form[name=newFiles]')).validate();

Name tags used are exemplary.


When you create the new element, just after your insert it, you just add the rules then, using .rules('add',rules), like this:

newField.YouJustCreatedrules("add", { required: true });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号