Enable/Disable a dropdownbox in jquery

后端 未结 10 1465
情深已故
情深已故 2020-12-01 03:34

I am new to jQuery and I want to enable and disable a dropdown list using a checkbox. This is my html:


                        
    
提交评论

  • 2020-12-01 04:06

    Here is one way that I hope is easy to understand:

    http://jsfiddle.net/tft4t/

    $(document).ready(function() {
     $("#chkdwn2").click(function() {
       if ($(this).is(":checked")) {
          $("#dropdown").prop("disabled", true);
       } else {
          $("#dropdown").prop("disabled", false);  
       }
     });
    });
    
    0 讨论(0)
  • 2020-12-01 04:06

    A better solution without if-else:

    $(document).ready(function() {
        $("#chkdwn2").click(function() {
            $("#dropdown").prop("disabled", this.checked);  
        });
    });
    
    0 讨论(0)
  • 2020-12-01 04:13

    try this

     <script type="text/javascript">
            $(document).ready(function () {
                $("#chkdwn2").click(function () {
                    if (this.checked)
                        $('#dropdown').attr('disabled', 'disabled');
                    else
                        $('#dropdown').removeAttr('disabled');
                });
            });
        </script>
    
    0 讨论(0)
  • 提交回复
    热议问题