19
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryで、Enterキーのサブミット禁止

Last updated at Posted at 2013-03-08

2018.01.15穴があったので修正

Enterを利かせたいもの以外を止める考えに変更しました。

   $("body").on("keydown", function (e) {
        if (e.keyCode !== 13) { return true;}
        if ($(document.activeElement).prop("tagName") == "TEXTAREA"
            && !$(document.activeElement).is(':disabled')
            && !$(document.activeElement).attr('readonly')
            )
        {
            return true;
        }
        if ($(document.activeElement).is('input[type="button"],input[type="submit"],a')) {
            return true;
        }
        return false;
    });

この考え方の場合、本文のテキストにフォーカスがある場合にエンターでSubmitするため。

TextBox等でEnterを入力するとSubmitするのを防止。

    <script type="text/javascript" >
        //エンターでのsubmitを禁止
        $(document).ready(function () {
            $('input,textarea[readonly]').not($('input[type="button"],input[type="submit"]')).keypress(function (e) {
                if (!e) var e = window.event;
                if (e.keyCode == 13)
                    return false;
            });
        });
    </script>
19
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?