防止表单提交preventDefault问题

时间:2014-12-18 15:02:37

标签: javascript jquery

我试图通过执行以下操作来阻止我的表单提交,但是当我点击提交按钮时它仍在继续提交:

$(document).ready(function($) {
    //When the form is submitted do this...
    $("#stripe-payment-form").submit(function(e) {
        e.preventDefault(); 
        // disable the submit button to prevent repeated clicks
        $('#stripe-submit').attr("disabled", "disabled");
        // send the card details to Stripe
        Stripe.createToken({
            number: $('.card-number').val(),
            cvc: $('.card-cvc').val(),
            exp_month: $('.card-expiry-month').val(),
            exp_year: $('.card-expiry-year').val()
        }, stripeResponseHandler);
        // prevent the form from submitting with the default action
        return false;
    });
});

谁能告诉我为什么?

1 个答案:

答案 0 :(得分:-1)

尝试阻止从"提交"提交表单按钮本身:

$(document).ready(function() {
    //When the form is submitted do this...
    $("#stripe-submit").on("click", function(e) {
        e.preventDefault(); 
        // disable the submit button to prevent repeated clicks
        $(this).attr("disabled", "disabled");
        // send the card details to Stripe
        Stripe.createToken({
            number: $('.card-number').val(),
            cvc: $('.card-cvc').val(),
            exp_month: $('.card-expiry-month').val(),
            exp_year: $('.card-expiry-year').val()
        }, stripeResponseHandler);
        // prevent the form from submitting with the default action
        return false;
    });
});

另外,请确保更正第一行:

$(document).ready(function($) {

要:

$(document).ready(function() {

......正如A. Wolff指出的那样:)