使用jQuery validate验证具有相同类的多个表单

7

我在一个页面上有大约10个相同类别的表单。每个表单都应该能够独立验证和发送。我正在使用jquery validate插件。但是我无法使其正常工作,因为所有表单都会提交第一个表单。除此之外,我似乎无法通过$(this).find('.error').html(error); 定位到表单内的errormessage div。

每个表单的结构如下:

<form method="post" class="alertform" action="">
<input type="text" onclick="clearText(this)" value="" class="required email" name="email">
<input type="hidden" value="1000004011320719" name="productid" class="productid">
<input type="submit" class="button" name="button" value="Set alert">
<div class="error">&nbsp;</div>
</form>

我的JS:

$('.alertform').each( function(){
$(this).validate({
rules: {
        emailadres: {
            required: true,
            email: true
            }
    },
    messages: {

                    emailadres: {
            required: "Message 1",
            minlength: "Message 2",
            email: "Message 3"
            }

            },
          errorPlacement: function(error, element) {
 $(this).find('.error').html(error);
},
success: function(label) {
label.addClass("valid").text("")
},
submitHandler: function() { 

  var emailadres = $(this).find("input.email").val();
  var productid = $(this).find("input.productid").val();

  var dataString = 'emailadres=' + emailadres + '&productid=' + productid;

    $.ajax({
  type: "POST",
  url: "/setalert.php",
  data: dataString,
  success: function(msg) {

    if (msg==1) {
    $(this).find(".email").attr("value", ""); 

    $(this).find('.error').html("Pricealert set.")


    }
    else {


    $(this).find('.error').html("Oops.")

          }

}
});

}
})
});

非常感谢您的帮助!

2个回答

14
我看了一下你的HTML和JS代码,你能试一下这个吗?
$('.alertform').each( function(){
   var form = $(this);
   form.validate({

基本上,将您代码中的所有 $(this) 都更改为 form。

这是关于 $(this) 的常见错误,有时它会丢失其引用。

希望有所帮助 :)


这解决了FF的问题。在IE中,如果您有有效的电子邮件地址,则可以正常工作。错误放置在IE中不起作用。 - dacross
我希望我能够这样做,但我需要15个声望才能这样做 :) 但它正在工作。我还更改了错误的位置,以使其在IE中正常工作:已更改:form.find('.error').html(error); 为:element.nextAll('div').html(error); - dacross

5

完整的JS代码:

$('.alertform').each( function(){

var form = $(this);
form.validate({
rules: {
        emailadres: {
            required: true,
            email: true
            }
    },
    messages: {

                    emailadres: {
            required: "Required",
            minlength: "Fill it in",
            email: "Not valid"
            }

            },
          errorPlacement: function(error, element) {
          element.nextAll('div').html(error);

},
success: function(label) {
 label.addClass("valid").text("")
},

submitHandler: function() { 

  var email = form.find("input.email").val();
  var productid = form.find("input.productid").val();

  var dataString = 'email=' + email + '&productid=' + productid;

    $.ajax({
  type: "POST",
  url: "/myurl.php",
  data: dataString,
  success: function(msg) {

    if (msg==1) {
    form.find(".email").attr("value", ""); 

    form.find('.error').html("Thanks")


    }
    else {


    form.find('.error').html("Something went wrong")

          }

  }
 });


}
})
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接