使用jquery工作提交表单值但成功后无法提交标准html表单

时间:2013-07-03 10:11:15

标签: jquery

我首先要为数据库中的插入发布一些ajax数据,这很好用,但是标准html表单的提交失败了:$(“#foo”)。submit(); 控制台消息无休止地循环: - /

<script type="text/javascript"> 
// variable to hold request var request; 
// bind to the submit event of our form $("#foo").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea, text, hidden");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    $inputs.prop("disabled", true);

    // fire off the request to /form.php
    request = $.ajax({
        url: "/insert_payment_data.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console         
    console.log("Hooray, it worked!");
    $("#foo").submit();
});

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault(); });

</script>

这让我抓狂: - (

1 个答案:

答案 0 :(得分:1)

这是正常的:

$("#foo").submit();

request.done方法末尾的这一行触发了表单的提交,这可能会触发AJAX请求,这会触发request.done ......等等无限: )

我会做什么(尝试)删除该行,并删除底部的event.preventDefault();语句,这样您的提交将在您执行AJAX请求后照常运行。< / p>