client side validation on dynamic elements
I have a form that a user can click a button to add another set of inputs. So I am using eName[] for example. I was hoping to use jquery开发者_JAVA百科 to do this, and thought live() would be a great way to loop through all the elements the user created to see if they are blank but I am not sure if I can do this without binding to an event. So I am thinking something like:
$(form).submit(funciton()({
//other validation here
$(".eName").live(bindToWhat?, function() {
//loop and check val();
});
});
Or maybe this is the wrong approach.
I hope this is enough info, if not lemme know. Thanks!!
Don't bind to inputs, do this instead:
$(form).submit(funciton()({
$(".eName").each(function() {
if ($(this).val() === ''){
alert('This field can not be empty');
$(this).focus();
}
});
});
Where eName
is supposed to be the class assigned to the fields.
精彩评论