【问题标题】:checkbox is not unchecking on the change event of the jquery复选框未取消选中 jquery 的更改事件
【发布时间】:2016-03-08 17:41:48
【问题描述】:

我在我的 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 语句中尝试将 = 更改为 === 您正在分配而不是使用此语句进行比较
  • thanx 先生.....这是我的一个愚蠢的错误.....

标签: javascript jquery asp.net checkbox


【解决方案1】:

在您的 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>

【讨论】:

    【解决方案2】:

    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);
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2015-01-31
      相关资源
      最近更新 更多