簡體   English   中英

復選框未取消選中 jquery 的更改事件

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

我在我的 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);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM