【问题标题】:Unable to submit the form via ajax call back function in jquery无法通过 jquery 中的 ajax 回调函数提交表单
【发布时间】:2013-03-08 09:34:59
【问题描述】:

我做了一个 ajax 来防止表单提交验证

我正在通过将 ajax 返回值存储在隐藏变量中来进行验证...

由于在我的 else 循环中返回 false,我的表单未提交

在 Ajax 返回成功后,我从 jquery 提交表单,但我得到了

未捕获的类型错误:对象# 的属性“提交”不是函数 请帮帮我.....

我只是在从 test.php 获取的隐藏字段中存储一个值 1 ....

但控制台在 $("#foo")[0].submit(); 上显示错误

如果我第二次点击提交,JavaScript 检查隐藏的值 = 1 并进入真正的循环......然后它得到提交

提前谢谢你

<form id="foo" name="foo" action="pop.php"  method="get">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />
    <input id="bar1" name="bar1" type="text" value="" />
    <input type="hidden" name="statushidden" id="statushidden" value=""/>
    <input type="submit" value="submit" id="submit" />
</form>

$(document).ready(function() {

    $("#submit").click(function() {
        var $form = $(this);
        var status = validdata();

        if (document.getElementById("statushidden").value=="1")
        {
            alert("true loop");
            return true;
        }
        else
        {
            alert("false loop");
            return false;
        }

    });

    function validdata()
    {
        var url = "test.php"; 
        $.ajax({
           type: "POST",
           beforeSend: function(){},
           url: url,
           data: $("#foo").serialize(), // serializes the form's elements.
           success: function(data)
           {
               document.getElementById("statushidden").value=data;
               console.log($("#foo")[0].submit);
               $("#foo")[0].submit(); ///// SHOWING Error on this line///////      
           }
        });
    }
});

【问题讨论】:

    标签: ajax jquery


    【解决方案1】:

    你需要告诉contentType:"Application/Json"

    在您的点击方法之前。您阻止默认操作。一旦 ajax 完成,您调用 submit 方法。

    $("#submit").click(function(e) {
             e.preventDefault();
            var $form = $(this);
            // your rest code. 
        });
    

    尝试这样的 ajax 方法。

    $.ajax({
    url:url,
    data:$("#foo").serialize(),
    contentType:"Application/Json";
    .... your success blocks. 
    });  
    

    如果您在提交方法方面遇到问题。然后像这样尝试

    $("form").submit(); ///// Error on this line will be fixed///////      
    

    【讨论】:

    • @user1386579 你的表单有 GET 方法。所以它没有发布数据。将其更改为 POST
    • 更改为在 jquery ajax 中的表单提交行上仍然显示错误.. 是否有任何其他语法可以从 jquery Ajax 提交表单
    【解决方案2】:

    请从下面的函数中删除 [0] 来检查

    function validdata()
        {
            var url = "test.php"; 
            $.ajax({
               type: "POST",
               beforeSend: function(){},
               url: url,
               data: $("#foo").serialize(), // serializes the form's elements.
               success: function(data)
               {
                   document.getElementById("statushidden").value=data;
                   $("#foo").submit(); ///// Removed [0] from your given snippet ///////      
               }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 2021-03-29
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多