Enable/Disable a dropdownbox in jquery

2019-01-08 16:15发布

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

<select id="dropdown" style="width:200px">
    <option value="feedback" name="aft_qst">After Quest</option>
    <option value="feedback" name="aft_exm">After Exam</option>
</select>
<input type="checkbox" id="chkdwn2" value="feedback" />

What jQuery code do I need to do this? Also searching for a good jQuery documentation/study material.

9条回答
祖国的老花朵
2楼-- · 2019-01-08 16:31

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);  
   }
 });
});
查看更多
看我几分像从前
3楼-- · 2019-01-08 16:35

A better solution without if-else:

$(document).ready(function() {
    $("#chkdwn2").click(function() {
        $("#dropdown").prop("disabled", this.checked);  
    });
});
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-08 16:36

To enable/disable -

$("#chkdwn2").change(function() { 
    if (this.checked) $("#dropdown").prop("disabled",true);
    else $("#dropdown").prop("disabled",false);
}) 

Demo - http://jsfiddle.net/tTX6E/

查看更多
在下西门庆
5楼-- · 2019-01-08 16:38

Try -

$('#chkdwn2').change(function(){
    if($(this).is(':checked'))
        $('#dropdown').removeAttr('disabled');
    else
        $('#dropdown').attr("disabled","disabled");
})
查看更多
叼着烟拽天下
6楼-- · 2019-01-08 16:49
$(document).ready(function() {
 $('#chkdwn2').click(function() {
   if ($('#chkdwn2').prop('checked')) {
      $('#dropdown').prop('disabled', true);
   } else {
      $('#dropdown').prop('disabled', false);  
   }
 });
});

making use of .prop in the if statement.

查看更多
Explosion°爆炸
7楼-- · 2019-01-08 16:51
$("#chkdwn2").change(function(){
       $("#dropdown").slideToggle();
});

JsFiddle

查看更多
登录 后发表回答