我的表單上有一個'輸入提交類型'按鈕。爲什麼'input submit'按鈕阻止jQuery提交表單?
該按鈕正在提交表單。
但是,當我加入jQuery的做一個動態提交它沒有工作,直到我更換輸入提交按鈕一個按鈕元素
<input type="submit" ...
到:
什麼的原因那?
動態表單提交現在可以與button
元素一起正常工作,但花了我一段時間才弄清楚原因。
我的表單上有一個'輸入提交類型'按鈕。爲什麼'input submit'按鈕阻止jQuery提交表單?
該按鈕正在提交表單。
但是,當我加入jQuery的做一個動態提交它沒有工作,直到我更換輸入提交按鈕一個按鈕元素
<input type="submit" ...
到:
什麼的原因那?
動態表單提交現在可以與button
元素一起正常工作,但花了我一段時間才弄清楚原因。
您可能需要在表單標記中添加onSubmit="return false;"
。
我不能給你比沒有看到你的代碼更好的答案。
要做一個JavaScript動態提交,你需要停止提交按鈕的默認動作發生。
$(function() {
$(':submit').click(function (event) {
event.preventDefault();
// submit the form dynamically
});
});
,而不是試圖使用窗體的submit
事件,如
$("#form-id").submit(function() {
var form = $(this);
var action = form.attr('action');
var method = form.attr('method');
var data = form.serialize();
/// you now have all the information required to
/// perform a dynamic or AJAX form submission
return false; // prevent default action
});
趕上點擊指定的表單元素,最可靠的方法來截取表單提交事件,則此方法不但捉單擊事件<input type="submit">
,<input type="image">
和<button type="submit">
元素,但當Enter
鍵被按下時,同時將焦點對準文本字段也將工作。
不酷安德魯 – Phil
@phil - 我的道歉。這對我來說有點不利。我已將我的答案改回原來的形式。 –