LoginSignup
1
2

More than 5 years have passed since last update.

JQueryで作る解除可能なラジオボタン(もう一度クリックで解除可能)

Posted at

汎用的に使えるように作ってみました。

 //解除可能なラジオボタン
  (function($) {
      $.fn.uncheckableRadio = function() {
          return this.each(function() {
              var radio = this,
                    label = $('label[for="' + radio.id + '"]');
              if (label.length === 0) {
                  label = $(radio).closest("label");
              }
              var label_radio = label.add(radio);
              label_radio.mousedown(function() {
                  $(radio).data('wasChecked', radio.checked);
              });
              label_radio.click(function() {
                  if ($(radio).data('wasChecked')) {
                      radio.checked = false;
                      $(radio).change(); //変更イベントを発生
                  }
              });
          });
      };
  })(jQuery);

  $(document).ready(function() {
      $('input[type=radio]').uncheckableRadio();
  });
1
2
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
1
2