回答
嘗試:
$("input[type='reset']").on("click", function(event){
alert('before reset: ' + $("input[type='text']").val());
// executes before the form has been reset
event.preventDefault();
// stops the form from resetting after this function
$(this).closest('form').get(0).reset();
// resets the form before continuing the function
alert('after reset: ' + $("input[type='text']").val());
// executes after the form has been reset
});
你可能想縮小這種形式的選擇到參與的ID的具體形式。
爲什麼downvote?我做錯了什麼? – iambriansreed 2012-04-25 16:33:31
不知道誰對你投了反對票,爲什麼,但我投了你票並接受了你的回答。謝謝。 – 2012-04-25 17:12:57
@BrianDavidBerman謝謝! – iambriansreed 2012-04-25 17:39:47
嘗試
$('your-form').bind('reset', function() {
alert('hi');
});
[這個鏈接](http://forum.jquery.com/topic/trapping-the-form-reset-event)應該提供背景和一些文件,說明這是如何和爲什麼會起作用的,還有爲什麼沒有更多在關於它的jQuery文檔中。 – 2012-04-25 16:02:24
在窗體重置之前執行。 – 2012-04-25 16:04:43
嘗試綁定事件內部的更改事件,然後,因爲下一個更改將是重置操作 – 2012-04-25 16:09:00
將這種幫助
$("input[type='reset']").on("click", function(){
alert("the form has been reset");
});
鏈接的jsfiddle:http://jsfiddle.net/sdkTz/1/
在表單重置之前執行此操作。 – 2012-04-25 16:05:46
一個click
事件添加到復位按鈕,並讓它看起來像這樣:
function(e) {
setTimeout(function() {
// your actual code here
},1);
}
更新:使用preventDefault
代替return false
。
$('input[type="reset"]').click(function(evt) {
// Prevent the reset button from firing the form's reset event again
evt.preventDefault();
$(this).closest('form').get(0).reset();
// At this point your form's inputs should have their values reset
});
'return false;'不僅僅是阻止提交表單。請參閱:http://stackoverflow.com/a/1357151/144665 – iambriansreed 2012-04-25 16:18:33
我相應地更新了我的答案。感謝您的支持。 – 2012-04-25 16:24:45
現在它看起來像我的,除了OP的警報呼叫。 :) – iambriansreed 2012-04-25 16:25:57
我特別不喜歡的重置事件綁定到復位按鈕,而不是形式的想法。表單可以通過其他方式重置,在這種情況下,您的事件不會觸發。
而是將函數綁定到重置事件,但將其置於瞬間setTimeout內。它將確保表格在調用函數之前實際被重置。
$('form').on('reset', function(e)
{
setTimeout(function() { /* ... */ });
});
雙加upvote。 – quillbreaker 2014-02-25 20:56:45
正在省略setTimeout通用支持的第二個參數嗎? w3schools和mozilla都將其列爲必需的參數。似乎在我的所有瀏覽器中都能正常工作,但我不確定較老的IE – Kip 2014-04-18 18:37:33
@Kip是的,它在較早的IE中運行得很好。沒有測試過IE6,但我並不擔心這一點。在嚴格模式下工作也很好。但是,添加零可能會增加代碼的清晰度。 – 2014-04-19 07:01:33
的建議是,與其用這種方式<input type='Reset'>
使用<input type = "button">
你沒有停止reset
按鈕的默認行爲。您只需將onclick
屬性添加到按鈕,並在該函數中您可以隨時調用窗體的重置方法,並根據需要控制行爲。下面的代碼說明了
HTML:
<input type="button" value="Limpiar" onclick="resetForm(this);"/>
的JavaScript:
function resetForm(element) {
//Do what you need before reset the form
element.form.reset(); //Reset manually the form
//Do what you need after reset the form
}
這裏使用類似於你如何正常使用onsubmit
事件onreset
事件爲例捕捉單擊提交按鈕的表單元素。爲了澄清,添加了onsubmit
示例。
在你的腳本:
function submitForm(form){
var xhr = new XMLHttpRequest(),
data = new FormData(form);
xhr.open("POST", url, true);
xhr.onload = function(event) {
switch (event.target.status){
case 200:
onSuccess(event);
break;
default:
onError(event);
}
};
xhr.send(data);
return false;
}
function resetForm(form){
var elements = form.elements;
// set some initial values to those form elements
return false;
}
在你的HTML:
<form onsubmit="return submitForm(this);" onreset="return resetForm(this);">
<button type="submit">Save</button>
<button type="reset">Reset</button>
</form>
- 1. 如何讓jQuery執行與html表單相同的操作
- 2. jQuery表單驗證代碼與HTML
- 3. 如何在執行頭後獲取HTML源代碼(位置:URL)
- 4. 如何在我的表單啓動後執行代碼?
- 5. JQuery的:如何發送表單之前執行代碼
- 6. 如何在jQuery的.html()中執行jquery/javascript代碼
- 7. 如何執行jQuery代碼
- 8. 是否可以在html表單後執行一些php代碼?
- 9. 的jQuery的代碼是如何執行
- 10. 如何從html表單執行ruby代碼?
- 11. dispatch_group_notify GCD後如何執行的代碼?
- 12. 如何在代碼後面設置HTML表格單元格值
- 13. 從後面的代碼執行與類關聯的jQuery對話
- 14. 如何在加載表單後執行代碼(Compact Framework 3.5)?
- 15. 如何在表單加載後執行代碼?
- 16. 後jQuery重置表單?
- 17. 如何讓jQuery Mobile在生成的代碼上重新執行?
- 18. 如何從html執行ruby代碼?
- 19. 如何在插入表單jQuery代碼的div上運行我的jquery代碼?
- 20. 執行代碼後PHP重定向
- 21. SyntaxError:missing}在jQuery代碼執行的屬性列表顯示後
- 22. 後成功執行jQuery代碼
- 23. 最後執行jQuery代碼塊
- 24. jquery msgBox在代碼後面執行(c#)
- 25. 執行簡單的代碼
- 26. 訂單執行代碼的
- 27. 如何從後面的asp.net代碼訪問HTML表單輸入
- 28. 執行HTML編碼的PHP代碼
- 29. 使用document.write不執行iframe後的任何HTML代碼
- 30. Django不會重新執行POST表單上的代碼
你可以綁定到表單的重置事件,但這種情況發生之前的輸入值獲得復位。輸入重置後沒有任何事件會立即運行。 – 2012-04-25 16:03:04
如果您必須等待輸入被重置,請在重置事件中執行一次setInterval,該事件在檢測到輸入值已被重置後自行清除,然後運行您的代碼。 – 2012-04-25 16:08:01
@OP你覺得有用嗎? – iambriansreed 2012-04-25 16:29:29