作者: | 2011/04/12 14:58:28 | 5 則迴響






<form id="new" action="" method="POST">
    <input type="text" id="code" name="code"> <!-- 代號輸入框 -->
    <input type="text" id="name" name="name"> <!-- 名稱出現框 -->
    <input type="submit" id="submit" value="提交" />


    // 偵測若在代號框輸入值後,按下Enter,就填進對應的名稱 
        // Enter鍵值為13
        if (e.keyCode == '13') {
            // 觸發填值的動作,在此省略


    // 偵測若在代號框輸入值後,按下Enter,就填進對應的名稱
        // Enter鍵值為13
        if (e.keyCode == '13') {
            // 觸發填值的動作,可能用get或load等ajax取值...

    // 表單的submit都會進來這兒
        e.preventDefault(); // 成功阻擋Enter的submit動作!

是的,成功阻擋了submit,但是若您手動去按提交鍵會發現submit動作也被擋了,再也提交不出表單了XD。於是有人會問,如何在使用過e.preventDefault後,再一次讓submit復活呢(reenable submit)!?? 唉,我也沒試出來XD,不過在此可以使用一個workaround的方式解決。

重新解讀我們想完成的事情就是–希望submit事件不要在代號框取得focus時會return true,讓它好好的做它該做的事:去把名稱填好。了解這樣的方式後,解決方式幾乎就呼之欲出了:

    // 用來判斷是否submit的flag
    var fSubmit = false;

    // 偵測若在代號框輸入值後,按下Enter,就填進對應的名稱
        // Enter鍵值為13
        if (e.keyCode == '13') {
            // 觸發填值的動作,可能用get或load等ajax取值
    }).focus(function(){ // 代號框取得focus,這時擋下所有的submit事件
        fSubmit = false;
        fSubmit = true; //代號框失去focus,這時允許submit

    // 表單的submit都會進來這兒
        return fSubmit; // 傳回true就會submit,傳回false就表示不作submit




本文作者是Audi Lu

5 則留言

  • sheephead says:

    ㄏ 你們的文章寫得很讚


  • alton says:

    Welcome to my blog ,
    I’m Alton.
    If you’ve ever been too tired and couldn’t finish a research paper , then you’ve come to the right place. I help students in all areas of the writing process . I can also write the paper from start to finish.
    My career as a professional writer started during my school years . After learning that I was very skilled in the field of academic writing, I decided to take it up as a profession.

    Skilled Academic Writer- Alton Sawyer- 10 Most Common College Freshman Mistakes Confederation

  • kalem says:

    Hello, I’m Kalem.
    Welcome to my homepage . I started writing in high school after a creative writing assignment for my English teacher. I did creative writing for almost a year before I thought about doing something else.
    I had always loved doing research assignments because I’m passionate about learning. When you combine writing ability with a love of learning, dissertation writing only makes sense as a job.
    I’m passionate about helping the students of the future in their school career. When they don’t have time for their paper , I am there to help.

    Kalem Warner – Professional Academic Writer – Writing recources hints Company

  • montell says:

    Hello, it’s Montell here!
    I work as an academic writer and have created this content with the intent of changing your life for the better. I started honing my writing skills in college. I learned that my fellow students needed writing help—and they were willing to pay for it. The money was enough to help pay my tuition for my first semester of college.
    Ever since high school, I have continued to work as a professional writer. I was hired by a writing company based in the United Kingdom. Since then, the dissertations that I have written have been sold around Europe and the United States.
    In my line of work, I have become accustomed to hearing, “Montell, can you help me meet my writing assignment deadline?” I know that I can save their time.

    Academic Writer – Montell Rivera – Seven tips for writers Corp

  • ianis says:

    Hello guys, my name is Ianis!

    I`m an academic writer and I`m going to change your lifes onсe and for all
    Writing has been my passion for a long time and now I can`t imagine my life without it.
    Most of my works were sold throughout Canada, USA, China and even India. Also I`m working with services that help people to save their nerves.
    People ask me “Please, Ianis, I need your professional help” and I always accept the request, `cause I know, that only I can solve all their problems!

    Academic Writer – Ianis Bowers – 7stepdesign.com Team






這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料