开发者

client side validation on dynamic elements

开发者 https://www.devze.com 2023-01-12 10:37 出处:网络
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

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.

0

精彩评论

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