2013-03-08 32 views
0

提交表单,我做了一个ajax防止用于验证的形式submition无法通过AJAX回调函数jQuery中

我通过存储ajaxs做验证返回隐变量...

我的表单不提交由于返回false我在其他环

阿贾克斯返回成功我从jQuery的递交表格后,但我收到

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

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

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

如果我点击提交第二次检查的JavaScript的隐藏价值= 1,并进入真正的环...那么其get提交

预先感谢您

<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///////  
      } 
     }); 
    } 
}); 

回答

1

你需要告诉contentType:"Application/Json"在你的点击方法之前使用

。您阻止默认操作。一旦ajax完成,你就调用submit方法。

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

尝试这样的方法阿贾克斯。

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

如果您有提交方法的麻烦。然后尝试这样

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

的错误消失,但形式不提交 – kavinhuh 2013-03-08 09:49:53

+0

@ user1386579在窗体中有GET方法请检查。所以它不会发布数据。将其更改为POST – 2013-03-08 09:52:37

+0

更改为帖子仍然显示错误表单提交行中的jQuery的Ajax .. 是否有任何其他语法提交表单从jQuery的Ajax – kavinhuh 2013-03-08 09:57:40

0

通过删除[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 ///////  
      } 
     }); 
    } 
+0

是啊完成没有用 – kavinhuh 2013-03-09 06:19:02