1

正如标题所说...

我的一些代码:有更多的输入,但我会放这个只是为了简化。

 <form name="frmCadastroPlano" id="frmCadastroPlano" class="form-horizontal"  >

 <input class="input-xlarge" id="inputNome" name="inputNome" type="text" value="" style="height: 26;">

 <button type="submit" class="btn btn-primary" id="enviar">Cadastrar</button>

 <button class="btn" name="clear" id="clear">Cancelar</button>



<div id="resposta"></div>

    </form>

阿贾克斯代码:

$("#frmCadastroPlano").submit( function(e) {
e.preventDefault();
dataString = $("#frmCadastroPlano").serialize();

$.ajax({
    type: "POST",
    url: "cadastroPlano.php",
    data : dataString,
    dataType: "html",

    success: function(retorno) {
        $("#resposta").html(retorno);
        resetForm('frmCadastroPlano');
    }

});
return false;
});

当我第一次按下按钮时,所有输入都在 url 中序列化,例如 GET Method... Example url.php?inputNome=asdas&inputDuracao=asdasda&inputPreco=asdas... 然后表单被重置,我必须再次输入数据,所以我终于可以提交了......我已经看到了这个类似问题的另一个答案,但没有运气:/ Sry English。

带有警报的输出(dataString) inputNome=asda&inputDuracao=dsadas&inputPreco=dasdas

4

3 回答 3

0

你什么时候运行这个ajax代码?看起来您没有在 document.ready 函数中运行它。尝试这个

$(document).ready(function() { 
    $("#frmCadastroPlano").submit( function(e) {
        e.preventDefault();
        dataString = $("#frmCadastroPlano").serialize();

        $.ajax({
            type: "POST",
            url: "cadastroPlano.php",
            data : dataString,
            dataType: "html",

            success: function(retorno) {
                $("#resposta").html(retorno);
                resetForm('frmCadastroPlano');
            }

        });
        return false;
    })
});

附带说明:使用浏览器中的开发人员控制台(例如,Chrome 中的 Control+Shift+I)查看网络上发生的情况(例如 GET 或 POST / which data / ...)。这可能会帮助您了解此类问题的根本原因。

您不妨看看这个 stackoverflow问题,因为在这种情况下,它看起来与您的问题相似。

于 2013-11-05T17:48:36.087 回答
0

尝试使用 HTML5 Form 对象 -

$("#frmCadastroPlano").submit( function(e) {
    e.preventDefault();
    dataString = new FormData($("#frmCadastroPlano")[0]);

$.ajax({
    type: "POST",
    url: "cadastroPlano.php",
    data : dataString,
    dataType: "html",
    processData: false,
    timeout: 40000,
    success: function(retorno) {
        $("#resposta").html(retorno);
        resetForm('frmCadastroPlano');
    }
});
return false;
});
于 2013-11-05T18:48:11.690 回答
0

您当时是否正在运行另一个脚本?可能是 chrome 或 firefox 扩展?

于 2013-11-05T19:00:39.550 回答