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 });
精彩评论