复选框不会取消选中 jQuery 的更改事件

checkbox is not unchecking on the change event of the jquery

本文关键字:事件 jQuery 取消 复选框      更新时间:2023-09-26

我有一个复选框和我的 asp.net 网站中的按钮,按钮CSS属性在页面加载时设置为隐藏,借助复选框我想显示和隐藏 asp.net 按钮,问题是复选框被选中但我无法取消选中它....请帮忙,我给了代码

<script>
        $(function () {
            $("#Btnforgotpass").css('display', 'none');
        });

        $(document).ready(function () {

            $("[id$=chkforgotpass]").change(function () {
                if (document.getElementById("chkforgotpass").checked = true) {
                    alert('checked');
                    $("[id$=Btnforgotpass]").css('display', 'inline');
                }
                else {
                    alert('unchecked');
                    $("[id$=Btnforgotpass]").css('display', 'none');
                }
            });
        });
</script>

在您的 if 语句上尝试将 = 更改为 === 您正在分配而不是使用此语句进行比较

$(function() {
  $("#Btnforgotpass").css('display', 'none');
});
$(document).ready(function() {
  $("[id$=chkforgotpass]").change(function() {
    if (document.getElementById("chkforgotpass").checked === true) {
      alert('checked');
      $("[id$=Btnforgotpass]").css('display', 'inline');
    } else {
      alert('unchecked');
      $("[id$=Btnforgotpass]").css('display', 'none');
    }
  });
});
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button id="Btnforgotpass">
    Button
  </button>
  <input type="checkbox" id="chkforgotpass">
</body>
</html>

if (document.getElementById("chkforgotpass").checked = true) {更改为if (document.getElementById("chkforgotpass").checked == true) {

如果你使用的是jQuery,这样做太简单了。

$(document).ready(function() {
   $("[id$=chkforgotpass]").change(function() {
       var isChecked = $(this).is(":checked") : "inline" : "none";
       $("[id$=Btnforgotpass]").css('display', isChecked);
  });
});