jquery 验证强制双击提交以提交到thickbox 的表单

发布于 2024-12-13 04:45:17 字数 1078 浏览 4 评论 0原文

我必须在表单上单击两次提交按钮才能在添加 jQuery 验证后提交它。还有其他与此相关的帖子,但这些解决方案对我不起作用,因为我的表单提交到厚盒弹出窗口,需要 GET 方法和传递某些变量。我正在使用 jquery.validate/1.8/jquery.validate.min.js 和 jquery/1.5.2/jquery.min.js。我不包括规则,因为它只是一个字段(数量),我使用“数字”类进行验证,以验证是否存在一个值并且它是一个数字。

这是我的代码:

$(document).ready(function(){
// validate the form when it is submitted
  $("form.cart_form").validate({


    //adding submitHandler results in having to click twice 
    submitHandler: function(form) {

        $("form.cart_form").submit(function() {
        var title = "";
        var productID = $("select[name=product_id]", this).val();
        var quantity = $("input[name=quantity]", this).val();
        var url = "../cart/add-to-cart.php?product_id=" + productID + "&quantity=" + quantity + "&TB_iframe=true&height=300&width=600";
        tb_show(title, url, false);
        // submit the form 
        // return false to prevent normal browser submit & page navigation 
        return false; 
            });
     }

  }); 

}); 

我绝不是 jQuery 专家,所以提前感谢您可以提供的任何解决方案。

I have to click the submit button twice on my form in order to submit it after adding jQuery validation. There are other posts regarding this but those solutions are not working for me as my form submits to a thickbox popup which requires a GET method and certain variables to be passed. I am using jquery.validate/1.8/jquery.validate.min.js and jquery/1.5.2/jquery.min.js. I am not including rules as it is just one field (quantity) that I am validating with class 'number' to verify there is a value and it is a number.

Here is my code:

$(document).ready(function(){
// validate the form when it is submitted
  $("form.cart_form").validate({


    //adding submitHandler results in having to click twice 
    submitHandler: function(form) {

        $("form.cart_form").submit(function() {
        var title = "";
        var productID = $("select[name=product_id]", this).val();
        var quantity = $("input[name=quantity]", this).val();
        var url = "../cart/add-to-cart.php?product_id=" + productID + "&quantity=" + quantity + "&TB_iframe=true&height=300&width=600";
        tb_show(title, url, false);
        // submit the form 
        // return false to prevent normal browser submit & page navigation 
        return false; 
            });
     }

  }); 

}); 

I am by no means a jQuery expert, so thank you in advance for any solutions you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-12-20 04:45:17

您的代码出现过多递归错误: $("form.cart_form").submit() 触发另一轮验证,导致另一次调用 SubmitHandler,瞧,递归。将其替换为 form.submit()

$(document).ready(function(){
// validate the form when it is submitted
$("form.cart_form").validate({


//adding submitHandler results in having to click twice 
submitHandler: function(form) {

    form.submit(function() {
    var title = "";
    var productID = $("select[name=product_id]", this).val();
    var quantity = $("input[name=quantity]", this).val();
    var url = "../cart/add-to-cart.php?product_id=" + productID + "&quantity=" + quantity + "&TB_iframe=true&height=300&width=600";
    tb_show(title, url, false);
    // submit the form 
    // return false to prevent normal browser submit & page navigation 
    return false; 
        });
 }

}); 

});

Your code in a too-much-recursion error: $("form.cart_form").submit() triggers another round of validation, resulting in another call to submitHandler, and voila, recursion. Replace that with form.submit()

$(document).ready(function(){
// validate the form when it is submitted
$("form.cart_form").validate({


//adding submitHandler results in having to click twice 
submitHandler: function(form) {

    form.submit(function() {
    var title = "";
    var productID = $("select[name=product_id]", this).val();
    var quantity = $("input[name=quantity]", this).val();
    var url = "../cart/add-to-cart.php?product_id=" + productID + "&quantity=" + quantity + "&TB_iframe=true&height=300&width=600";
    tb_show(title, url, false);
    // submit the form 
    // return false to prevent normal browser submit & page navigation 
    return false; 
        });
 }

}); 

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文