<i id='eZ9c5'><tr id='eZ9c5'><dt id='eZ9c5'><q id='eZ9c5'><span id='eZ9c5'><b id='eZ9c5'><form id='eZ9c5'><ins id='eZ9c5'></ins><ul id='eZ9c5'></ul><sub id='eZ9c5'></sub></form><legend id='eZ9c5'></legend><bdo id='eZ9c5'><pre id='eZ9c5'><center id='eZ9c5'></center></pre></bdo></b><th id='eZ9c5'></th></span></q></dt></tr></i><div id='eZ9c5'><tfoot id='eZ9c5'></tfoot><dl id='eZ9c5'><fieldset id='eZ9c5'></fieldset></dl></div>

  • <legend id='eZ9c5'><style id='eZ9c5'><dir id='eZ9c5'><q id='eZ9c5'></q></dir></style></legend>

      <bdo id='eZ9c5'></bdo><ul id='eZ9c5'></ul>
  • <tfoot id='eZ9c5'></tfoot>

    <small id='eZ9c5'></small><noframes id='eZ9c5'>

        如何在单击时切换单选输入元素的检查状态?

        时间:2023-10-20

        <legend id='vOQ5U'><style id='vOQ5U'><dir id='vOQ5U'><q id='vOQ5U'></q></dir></style></legend>

        • <tfoot id='vOQ5U'></tfoot>

              <bdo id='vOQ5U'></bdo><ul id='vOQ5U'></ul>

                <small id='vOQ5U'></small><noframes id='vOQ5U'>

                  <tbody id='vOQ5U'></tbody>
              • <i id='vOQ5U'><tr id='vOQ5U'><dt id='vOQ5U'><q id='vOQ5U'><span id='vOQ5U'><b id='vOQ5U'><form id='vOQ5U'><ins id='vOQ5U'></ins><ul id='vOQ5U'></ul><sub id='vOQ5U'></sub></form><legend id='vOQ5U'></legend><bdo id='vOQ5U'><pre id='vOQ5U'><center id='vOQ5U'></center></pre></bdo></b><th id='vOQ5U'></th></span></q></dt></tr></i><div id='vOQ5U'><tfoot id='vOQ5U'></tfoot><dl id='vOQ5U'><fieldset id='vOQ5U'></fieldset></dl></div>

                1. 本文介绍了如何在单击时切换单选输入元素的检查状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在单击元素或其容器时(取消)检查单选输入元素?

                  How to (un)check a radio input element on click of the element or its container?

                  我已经尝试了下面的代码,但它并没有取消选中收音机.

                  I have tried the code below, but it does not uncheck the radio.

                  HTML:

                  <div class="is">
                      <label><input type="radio" name="check" class="il-radio" /> Is </label>
                      <img src="picture" />
                  </div>
                  

                  jQuery:

                  $(".is label, .is ").click(function () {
                      if (!$(this).find(".il-radio").attr("checked")) {
                          $(this).find(".il-radio").attr("checked", "checked");
                      } else if ($(this).find(".il-radio").attr("checked") == "checked") {
                          $(this).find(".il-radio").removeAttr("checked");
                      }
                  });
                  

                  推荐答案

                  你必须阻止默认行为.目前,点击时会发生以下情况:

                  You have to prevent the default behaviour. Currently, on click, the following happens:

                  1. click 事件触发容器 (div.is).
                  2. click 事件触发标签.
                  3. 由于您的函数切换了一个状态,并且事件监听器被调用了两次,结果似乎什么都没有发生.
                  1. click event fires for the container (div.is).
                  2. click event fires for the label.
                  3. Since your function toggles a state, and the event listener is called twice, the outcome is that nothing seems to happen.

                  更正的代码(http://jsfiddle.net/nHvsf/3/):p>

                  Corrected code (http://jsfiddle.net/nHvsf/3/):

                  $(".is").click(function(event) {
                      var radio_selector = 'input[type="radio"]',
                          $radio;
                  
                      // Ignore the event when the radio input is clicked.
                      if (!$(event.target).is(radio_selector)) {
                          $radio = $(this).find(radio_selector);
                          // Prevent the event to be triggered
                          // on another element, for the same click
                          event.stopImmediatePropagation();
                          // We manually check the box, so prevent default
                          event.preventDefault();
                          $radio.prop('checked', !$radio.is(':checked'));
                      }
                  });
                  $(".il-radio").on('change click', function(event) {
                      // The change event only fires when the checkbox state changes
                      // The click event always fires
                  
                      // When the radio is already checked, this event will fire only once,
                      //   resulting in an unchecked checkbox.
                      // When the radio is not checked already, this event fires twice
                      //   so that the state does not change
                      this.checked = !this.checked;
                  });
                  

                  这篇关于如何在单击时切换单选输入元素的检查状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何统一来自多组单选按钮和多组复选框的文本? 下一篇:JavaScript 读取 IE 和 FireFox 中的单选按钮值

                  相关文章

                2. <tfoot id='ERcgL'></tfoot>
                  <legend id='ERcgL'><style id='ERcgL'><dir id='ERcgL'><q id='ERcgL'></q></dir></style></legend>

                        <bdo id='ERcgL'></bdo><ul id='ERcgL'></ul>
                      <i id='ERcgL'><tr id='ERcgL'><dt id='ERcgL'><q id='ERcgL'><span id='ERcgL'><b id='ERcgL'><form id='ERcgL'><ins id='ERcgL'></ins><ul id='ERcgL'></ul><sub id='ERcgL'></sub></form><legend id='ERcgL'></legend><bdo id='ERcgL'><pre id='ERcgL'><center id='ERcgL'></center></pre></bdo></b><th id='ERcgL'></th></span></q></dt></tr></i><div id='ERcgL'><tfoot id='ERcgL'></tfoot><dl id='ERcgL'><fieldset id='ERcgL'></fieldset></dl></div>

                      <small id='ERcgL'></small><noframes id='ERcgL'>