LoginSignup
9

More than 5 years have passed since last update.

posted at

updated at

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

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 さんありがとう!

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
What you can do with signing up
9