LoginSignup
12
9

More than 5 years have passed since last update.

Submit時にサブミットボタンを殺す

Last updated at Posted at 2016-10-27

CMSとかでよくやる処理だけど、よく忘れてそのたびに一から作ってる気がするのでメモ。

Native JavaScript

(function () {

    document.addEventListener('submit', function (e) {
        var submits = document.querySelectorAll('form [type=submit]');
        Array.prototype.forEach.call(submits, function(e) {
            e.setAttribute('disabled', true);
        });

    });

}());

jQuery

$(function(){
    $(document).on('submit', function(event) {
      $('form').find(':submit').prop('disabled', true);
    });
});

変更履歴

  • Jqueryの処理をattrからpropに変更しました。
    • kanicraft さんありがとう!
  • console.logが入ったままだったので削除しました。
  • Native の submitボタン取得処理を修正しました。
    • vicboss1002 さんありがとう!
12
9
2

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
12
9