2011-11-15 22 views

回答

22

如果您正在使用該表單,則可以使用提交事件。

使用jQuery,你可以做到這一點與

$('#myform').submit(function() { 
    // your code here 
}); 
+7

此方法從來不適用於我。表格只是照常提交。 – americanknight

+1

@americanknight您需要使用preventDefault方法來停止按鈕操作。 ('#myform')。submit(function(e){ e。preventDefault(); }); – ius

5

您可以將事件處理程序綁定到submit事件(以下代碼假設你有一個id在你的form):

document.getElementById("someForm").onsubmit = function() { 
    //Do stuff 
}; 
29

是這樣的?

<form onsubmit="do_something()"> 

function do_something(){ 
    // Do your stuff here 
} 

如果你把return像下面的代碼,你可以從do_something()函數返回false防止表單提交。

<form onsubmit="return do_something()"> 

function do_something(){ 
    // Do your stuff here 
    return true; // submit the form 

    return false; // don't submit the form 
} 
相關問題