2012-04-25 32 views
32

點擊一個HTML復位按鈕後,如何執行後的HTML表單重置與jQuery的代碼?

<input type="reset" /> 

我想執行一些代碼。我該如何做到這一點,並確保在這樣做之前重置表單?

+0

你可以綁定到表單的重置事件,但這種情況發生之前的輸入值獲得復位。輸入重置後沒有任何事件會立即運行。 – 2012-04-25 16:03:04

+0

如果您必須等待輸入被重置,請在重置事件中執行一次setInterval,該事件在檢測到輸入值已被重置後自行清除,然後運行您的代碼。 – 2012-04-25 16:08:01

+0

@OP你覺得有用嗎? – iambriansreed 2012-04-25 16:29:29

回答

39

嘗試:

$("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的具體形式。

小提琴證明:http://jsfiddle.net/iambriansreed/Zh5cd/

+0

爲什麼downvote?我做錯了什麼? – iambriansreed 2012-04-25 16:33:31

+0

不知道誰對你投了反對票,爲什麼,但我投了你票並接受了你的回答。謝謝。 – 2012-04-25 17:12:57

+0

@BrianDavidBerman謝謝! – iambriansreed 2012-04-25 17:39:47

0

嘗試

$('your-form').bind('reset', function() { 
    alert('hi'); 
}); 
+0

[這個鏈接](http://forum.jquery.com/topic/trapping-the-form-reset-event)應該提供背景和一些文件,說明這是如何和爲什麼會起作用的,還有爲什麼沒有更多在關於它的jQuery文檔中。 – 2012-04-25 16:02:24

+0

在窗體重置之前執行。 – 2012-04-25 16:04:43

+0

嘗試綁定事件內部的更改事件,然後,因爲下一個更改將是重置操作 – 2012-04-25 16:09:00

-1

將這種幫助

$("input[type='reset']").on("click", function(){ 
    alert("the form has been reset"); 
    }); 

鏈接的jsfiddle:http://jsfiddle.net/sdkTz/1/

+1

在表單重置之前執行此操作。 – 2012-04-25 16:05:46

-5

一個click事件添加到復位按鈕,並讓它看起來像這樣:

function(e) { 
    setTimeout(function() { 
     // your actual code here 
    },1); 
} 
2

更新:使用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 
}); 

http://jsfiddle.net/EYqrX/1/

+0

'return false;'不僅僅是阻止提交表單。請參閱:http://stackoverflow.com/a/1357151/144665 – iambriansreed 2012-04-25 16:18:33

+1

我相應地更新了我的答案。感謝您的支持。 – 2012-04-25 16:24:45

+0

現在它看起來像我的,除了OP的警報呼叫。 :) – iambriansreed 2012-04-25 16:25:57

119

我特別不喜歡的重置事件綁定到復位按鈕,而不是形式的想法。表單可以通過其他方式重置,在這種情況下,您的事件不會觸發。

而是將函數綁定到重置事件,但將其置於瞬間setTimeout內。它將確保表格在調用函數之前實際被重置。

$('form').on('reset', function(e) 
{ 
    setTimeout(function() { /* ... */ }); 
}); 
+0

雙加upvote。 – quillbreaker 2014-02-25 20:56:45

+0

正在省略setTimeout通用支持的第二個參數嗎? w3schools和mozilla都將其列爲必需的參數。似乎在我的所有瀏覽器中都能正常工作,但我不確定較老的IE – Kip 2014-04-18 18:37:33

+1

@Kip是的,它在較早的IE中運行得很好。沒有測試過IE6,但我並不擔心這一點。在嚴格模式下工作也很好。但是,添加零可能會增加代碼的清晰度。 – 2014-04-19 07:01:33

1

的建議是,與其用這種方式<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 
} 
0

這裏使用類似於你如何正常使用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> 
相關問題