2011-08-30 188 views
0

我想提交所有窗體的信息與onclick事件功能(我不想使用傳統的提交)。我如何使用post或ajax方法?單擊提交表單,但不提交使用jquery和ajax

這裏是我的代碼

$("#login_form").bind("click", function() { 
    var qtyVal = document.getElementsByName('id[]').length; 

    $.ajax({ 
     type  : "post", 
     cache : false, 
     url  : "abcd.php?ref=xyz", 
     data  : $(this).serializeArray(), 
     success: function(data) { 
     $.fancybox(data); 
     } 
    }); 
    return false; 
}); 
+0

究竟應該點擊什麼元素? –

+0

什麼不行?你應該編輯你的問題,並更精確。 – JMax

回答

0

你不應該從this這裏使用關鍵字。你點擊一個按鈕發送表單數據,所以this會返回你的按鈕元素。您也不應該使用「點擊」事件。你可以做如下代碼:

function send(){ 

$.ajax({ 
    type  : "post", 
    cache : false, 
    url  : "abcd.php?ref=xyz", 
    data : $('#frm').serializeArray(), 
    success: function(data) { 
    alert(data); 
    } 
}); 
return false;} 



<form id="frm"><input type="button" id="submit" onclick="send()" /></form> 
+0

謝謝你的答案。 – jassi

0

您是否在尋找.submit()

如果login_form真的形式(即<form id="login_form">),你可以這樣做:

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

提交表單中的常規方式,而不AJAX調用。

+0

不,我想提交一個表單使用onclick()與Ajax和jQuery,而不使用表單提交() – jassi