Why is the checkbox not updating visually after using jQuery to uncheck it when clicking 'No' on a modal box?

huangapple go评论65阅读模式
英文:

Why is the checkbox not updating visually after using jQuery to uncheck it when clicking 'No' on a modal box?

问题

当我点击模态框的"是"按钮时,我试图在jQuery中取消选中输入复选框,并在点击"否"按钮时选中它。

我可以在开发者工具中看到,当点击"是"时,已添加"checked"属性(checked = "checked"),当点击"否"时,已隐藏,但在用户界面中,选中标记没有更新。

我尝试过以下代码在"是"按钮中:

$(this).prop("checked", true);

以及:

$(this).attr("checked", true);

但都没有成功。

英文:

I am trying to uncheck input checkbox in jquery when i click No button of modal box and check it I click Yes button .

$('body').off('change', '[name="techOptions"][value="8"]').on('change', '[name="techOptions"][value="8"]', function() {
      if ($(this).is(':checked')) {
        var $n = noty({
          text: 'Please provide relevant complementary information about .....',
          modal: true,
          layout: 'center',
          type: 'neutral',
          buttons: [{
            addClass: 'btn btn-default',
            text: 'Ok',
            onClick: function($noty) {
              $(this).prop("checked","checked");
              $noty.close();
            }
          },
           {
             addClass: 'btn btn-default',
             text: 'No',
             onClick: function ($noty) {
                $(this).removeAttr('checked');
                $noty.close();
              }
            }]
        });
        // Focus on "OK" button
        $n.$buttons.find('#button-0').first().focus();
        return false;
      }
    });

I can see in developer tool the checked is added(checked = "checked") when clicking yes and hidden when clicking no, but in the UI the checking mark is not updating.

I tried $(this).prop("checked","checked");
and $(this).prop("checked",true);
and also $(this).attr("checked",true);
in Yes button, but without success .

答案1

得分: 0

这段代码适用于您的情况。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <title>Checkbox and Modal Box</title>
</head>
<body>
  <div class="container">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" name="techOptions" value="8" id="techCheckbox">
      <label class="form-check-label" for="techCheckbox">
        Tech Option
      </label>
    </div>

    <!-- Modal Box -->
    <div class="modal" tabindex="-1" role="dialog" id="myModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-body">
            <p>请提供有关.....的相关补充信息。</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" id="modal-ok">是</button>
            <button type="button" class="btn btn-secondary" id="modal-no">否</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  <script>
    $(document).ready(function() {
      $('body').off('change', '[name="techOptions"][value="8"]').on('change', '[name="techOptions"][value="8"]', function() {
        if ($(this).is(':checked')) {
          var $checkbox = $(this); // 将复选框元素存储在一个变量中

          $('#myModal').modal('show'); // 显示模态框

          // 处理“是”按钮点击
          $('#modal-ok').on('click', function() {
            $checkbox.prop("checked", true); // 勾选复选框
            $('#myModal').modal('hide'); // 隐藏模态框
          });

          // 处理“否”按钮点击
          $('#modal-no').on('click', function() {
            $checkbox.prop("checked", false); // 取消勾选复选框
            $('#myModal').modal('hide'); // 隐藏模态框
          });
        }
      });
    });
  </script>
</body>
</html>

希望这对您有所帮助。

英文:

This code work for your case

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot;&gt;
&lt;title&gt;Checkbox and Modal Box&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; name=&quot;techOptions&quot; value=&quot;8&quot; id=&quot;techCheckbox&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;techCheckbox&quot;&gt;
Tech Option
&lt;/label&gt;
&lt;/div&gt;
&lt;!-- Modal Box --&gt;
&lt;div class=&quot;modal&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; id=&quot;myModal&quot;&gt;
&lt;div class=&quot;modal-dialog&quot; role=&quot;document&quot;&gt;
&lt;div class=&quot;modal-content&quot;&gt;
&lt;div class=&quot;modal-body&quot;&gt;
&lt;p&gt;Please provide relevant complementary information about .....&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-footer&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot; id=&quot;modal-ok&quot;&gt;Yes&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; id=&quot;modal-no&quot;&gt;No&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.5.1.slim.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function() {
$(&#39;body&#39;).off(&#39;change&#39;, &#39;[name=&quot;techOptions&quot;][value=&quot;8&quot;]&#39;).on(&#39;change&#39;, &#39;[name=&quot;techOptions&quot;][value=&quot;8&quot;]&#39;, function() {
if ($(this).is(&#39;:checked&#39;)) {
var $checkbox = $(this); // Store the checkbox element in a variable
$(&#39;#myModal&#39;).modal(&#39;show&#39;); // Show the modal box
// Handle &quot;Yes&quot; button click
$(&#39;#modal-ok&#39;).on(&#39;click&#39;, function() {
$checkbox.prop(&quot;checked&quot;, true); // Check the checkbox
$(&#39;#myModal&#39;).modal(&#39;hide&#39;); // Hide the modal box
});
// Handle &quot;No&quot; button click
$(&#39;#modal-no&#39;).on(&#39;click&#39;, function() {
$checkbox.prop(&quot;checked&quot;, false); // Uncheck the checkbox
$(&#39;#myModal&#39;).modal(&#39;hide&#39;); // Hide the modal box
});
}
});
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月29日 05:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定