2011-04-28 60 views
0

我想要一個文本形式輸入一個字符串,後來通過JavaScript讀取。基本的HTML文本形式

<form style='margin:10px;'> 
Input Value: <input type="text" value="3" id="input" name="input"/> 
</form> 

我注意到,當我按下Enter時,它被選中導致頁面被重新加載。這不是我想要的。當表單被「提交」時,如何讓它不能重新加載頁面?

+0

可能的重複的[如何防止ENTER鍵提交Web窗體?](http://stackoverflow.com/questions/585396/how-to-prevent-enter-keypress-to- submit-a-web-form) – 2011-04-28 08:12:32

+0

Duplicates:http://stackoverflow.com/questions/585396/how-to-prevent-enter-keypress-to-submit-a-web-form,http://stackoverflow.com/questions/895171/prevent-users-from-submitting-form-b​​y-hitting-enter,http://stackoverflow.com/questions/1563062/jquery-prevent-form-submition-with-enter-key ... – 2011-04-28 08:13:09

+0

可能的重複實際上是:http://stackoverflow.com/search?q=form+prevent+enter – 2011-04-28 08:15:08

回答

1

你需要的<form>標籤?他們似乎沒有做任何事情。如果你刪除它們,當你按下回車鍵時你將不再獲得提交行爲。

0

按enter鍵提交表單。您可以使用JavaScript來防止表單被提交 - 這樣做的一個方法是使用一個提交按鈕:

<input type="submit" onsubmit="formhandle(); return false;"> 

創建formhandle的Javascript()函數來完成你想做的處理。返回false應該阻止表單回發到服務器 - 但是,這並不總是有效。有一個關於防止默認操作在瀏覽器中點擊這裏更詳細的信息:

http://www.quirksmode.org/js/events_early.html

0

你必須抓住的形式使用JScript發送和它使用Ajax發送到服務器

<form style='margin:10px;' id='formID'> 
Input Value: <input type="text" value="3" id="input" name="input"/> 
</form> 

(你可以使用原型或純JS如果你想)代碼進入豆蔻像這樣

JQuery的
$('#target').submit(function() { 
    $.ajax({ 
    type: 'POST', 
    url: url, 
    data: data, 
    success: success, 
    dataType: dataType, 
    }); 
    return false; 
}); 

返回false可防止頁面重新加載。文檔可以發現here