简体   繁体   English

复选框未取消选中 jquery 的更改事件

[英]checkbox is not unchecking on the change event of the jquery

i have a checkbox and the button in my asp.net website,the button css property is set to hidden on the loading of the page,with the help of the checkbox i want to show and hide the asp.net button,the problem is that the check box is checked but i am not able to uncheck it....plz help i have give the code我在我的 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>

on your if statement try changing = to === you are assigning not comparing using this statement在您的 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>

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

if you are using jQuery, it is too simple doing this.如果您使用的是 jQuery,那么这样做太简单了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM