[英]Submit a form on click but not submit using jquery and ajax
我想使用onclick事件函數提交所有表單的信息(我不想使用常規的Submit)。 如何使用post或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;
});
您在尋找.submit()
嗎?
如果login_form
確實是一種表單(即<form id="login_form">
),則可以執行以下操作:
$("#login_form").submit();
以常規方式提交表格,而無需AJAX調用。
您不應在this
使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.