Loading...

使用jQuery阻止input直接按Enter就送出表單

Temperature: 0 °C

Mark ChangMark Chang
author_tools

使用表單是傳送資料裡經常會用到的方式。

這時當使用者輸入表單後習慣上按下Enter,則會將表單內容送出(submit)。

這往往會造成一些困擾,例如表單資料尚未完成?

要阻止這個預設的行為,可以使用jQuery來阻擋:

$('.input').keypress(function(e) {
code = e.keyCode ? e.keyCode : e.which; // in case of browser compatibility
if(code == 13) {
e.preventDefault();
// do something
/* also can use return false; instead. */
}
});

參考網址Url link

以上紀錄~
 

https://innstory.com/story-使用jQuery阻止input直接按Enter就送出表單-1904
javascript

Prev
 使用JavaScript的split函式完成字串切割

Next
JavaScript_取得今天日期 

About the Author

Mark Chang

離不開電腦的宅男

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

RPO(Relative Path Overwrite)相對路徑覆蓋攻擊

RPO(Relative P...

首先,先來用例子說明何謂「相對路徑」何謂「絕對路徑」。 例如你有一個網址為http://www.de...

javascript獲取當前時間

javascript獲取當前...

watchclock var myDate = new Date(); //獲取當前年 var ye...

在PHP中如何將NAN轉換為0

在PHP中如何將NAN轉換為...

首先我會得到NAN是因為我有一個計算結果為的值然後我讓他除以,結果就得到了NAN 首先甚麼是NAN?...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format