Javascript 如何在Ajax调用后提交表单

Javascript 如何在Ajax调用后提交表单,javascript,jquery,ajax,forms,Javascript,Jquery,Ajax,Forms,我有一张这样的表格 <form id="formCategorias" action="./index.php" method="post"> <input type="hidden" value="5" name="pais"> <table> <tbody> <tr class="row"> <td>Renta Comerciales</td>

我有一张这样的表格

<form id="formCategorias" action="./index.php" method="post">
<input type="hidden" value="5" name="pais">
<table>
    <tbody>
        <tr class="row">
            <td>Renta Comerciales</td>
            <td>
                <input type="text" class="validate" value="" id="inputcat_0" name="" style="border: 1px solid rgb(221, 221, 221);">
                <select id="categoria_0" name="categoria_0">
                    <option value="0">Provincias</option>
                    <option value="Oficinas">Oficinas (11)</option>
                    <option value="Ranchos">Ranchos (12)</option>
                    <option value="Naves Industriales">Naves Industriales (15)</option>
                </select>
                <button onclick="seleccionarCategoria(0)" type="button">+</button>
            </td>
        </tr>
        <tr>
            <td align="right" colspan="2">
                <input type="submit" id="categorias" value="Enviar" name="submit">
            </td>
        </tr>
    </tbody>
</table>
问题是在ajax调用之后,当我使用submit()函数提交表单时,浏览器重复执行ajax调用(),导致错误

修复方法可能是,submit()函数应该根据action标记中提供的url而不是ajax调用的url提交表单

谁能给我提点建议吗


提前感谢

这里有两个问题

  • 由于您正在调用jQuery submit事件,它将再次触发submit处理程序,创建一个无限递归调用。要修复此问题,请调用表单元素(dom对象)的submit方法
  • 因为ajax是异步的,所以请检查post回调本身中的有效条件
  • 所以


    我的方法如下

  • 编写ajax调用

    $request=$ajax(…)

    不要在这里写入.done或success:ajax中的参数

  • 从ajax URL在回调中发送一个值,该值为true或false

    $request.done(function(resp) {
      if ( resp.parameter === true )
        $('#formID').submit();
    });
    
    $request.fail( function () {
        alert('ajax fails');
     });
    

  • 备选方案:在jquery 1.5中添加的中使用
    数据过滤器:
    参数

    演示代码:

    $response = $.ajax( {
        .....
        dataType: 'JSON',
        dataFilter: function(result, type) {
                // console.group('DataFilter');
                // console.log(result);
                // console.log(type);
                // console.groupEnd();
                if (type === 'JSON') {
                    var parsed_data = $.parseJSON(result);
                    // console.info('Parsed Data');
                    // console.log(parsed_data);   
                    // code to pre-filtering function to sanitize the response.
                    $finalThing = JSON.stringify({ everyThing : true });
                    return $finalThing;
                } // end of if
            } // end of DataFilter
    }); // end of $.ajax
    
    $response.done (function(resp) {
        // here now `resp` will have $finalThing which comes from dataFilter
        if( resp.everyThing == true ) { $('#formId').submit(); }
    });
    

    这是一个预过滤函数,用于清理响应。你应该 返回经过清理的数据。该函数接受两个参数:原始参数 从服务器返回的数据和“dataType”参数


    在你的php中,你有没有简单地给我们一个回音


    Xavier

    通过执行底部的this.submit()调用本机submit。AJAX调用附加到表单的
    submit()
    事件。因此,如果不触发此事件,则无法正常提交表单。如果您想要这种行为,您需要更改逻辑。提示:在
    $中设置
    数据类型
    参数。ajax
    它的意思是frm.submit()不是function@Vikram警报是否
    警报(“#”+frm.id)显示的值是否正确?@diEcho这是问题的根源。。。由于这将重新触发处理程序…@Vikram,问题是提交按钮的名称。。。将其更改为除提交之外的任何内容,如
    ,则应为fine@diEcho这也不能解决问题。。。由于服务器返回一个失败,那么您应该能够再次提交表单,再次需要触发提交处理程序。。。如果使用
    one
    ,则第二次处理程序将不会被调用
    $request.done(function(resp) {
      if ( resp.parameter === true )
        $('#formID').submit();
    });
    
    $request.fail( function () {
        alert('ajax fails');
     });
    
    $response = $.ajax( {
        .....
        dataType: 'JSON',
        dataFilter: function(result, type) {
                // console.group('DataFilter');
                // console.log(result);
                // console.log(type);
                // console.groupEnd();
                if (type === 'JSON') {
                    var parsed_data = $.parseJSON(result);
                    // console.info('Parsed Data');
                    // console.log(parsed_data);   
                    // code to pre-filtering function to sanitize the response.
                    $finalThing = JSON.stringify({ everyThing : true });
                    return $finalThing;
                } // end of if
            } // end of DataFilter
    }); // end of $.ajax
    
    $response.done (function(resp) {
        // here now `resp` will have $finalThing which comes from dataFilter
        if( resp.everyThing == true ) { $('#formId').submit(); }
    });