在 jquery 中启用/禁用下拉框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7703241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable/Disable a dropdownbox in jquery
提问by ARUN P.S
I am new to jQuery and I want to enable and disable a dropdown list using a checkbox. This is my html:
我是 jQuery 的新手,我想使用复选框启用和禁用下拉列表。这是我的 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.
我需要什么 jQuery 代码才能做到这一点?也在寻找一个好的 jQuery 文档/学习材料。
回答by Kris Krause
Here is one way that I hope is easy to understand:
这是我希望易于理解的一种方式:
$(document).ready(function() {
$("#chkdwn2").click(function() {
if ($(this).is(":checked")) {
$("#dropdown").prop("disabled", true);
} else {
$("#dropdown").prop("disabled", false);
}
});
});
回答by Jayendra
Try -
尝试 -
$('#chkdwn2').change(function(){
if($(this).is(':checked'))
$('#dropdown').removeAttr('disabled');
else
$('#dropdown').attr("disabled","disabled");
})
回答by Chris Rosete
I am using JQuery > 1.8 and this works for me...
我正在使用 JQuery > 1.8,这对我有用...
$('#dropDownId').attr('disabled', true);
回答by Muhammad Ahmad
A better solution without if-else:
没有 if-else 的更好解决方案:
$(document).ready(function() {
$("#chkdwn2").click(function() {
$("#dropdown").prop("disabled", this.checked);
});
});
回答by santosh singh
try this
尝试这个
<script type="text/javascript">
$(document).ready(function () {
$("#chkdwn2").click(function () {
if (this.checked)
$('#dropdown').attr('disabled', 'disabled');
else
$('#dropdown').removeAttr('disabled');
});
});
</script>
回答by ipr101
To enable/disable -
启用/禁用 -
$("#chkdwn2").change(function() {
if (this.checked) $("#dropdown").prop("disabled",true);
else $("#dropdown").prop("disabled",false);
})
Demo - http://jsfiddle.net/tTX6E/
回答by Baqer Naqvi
$("#chkdwn2").change(function() {
if (this.checked) $("#dropdown").prop("disabled",'disabled');
})
回答by RossGamble
$(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.
利用的.prop
在if
声明。
回答by mohammad
this is to disable dropdown2 , dropdown 3 if you select the option from dropdown1 that has the value 15
这是禁用 dropdown2 , dropdown 3 如果您从 dropdown1 中选择值为 15 的选项
$("#dropdown1").change(function(){
if ( $(this).val()!= "15" ) {
$("#dropdown2").attr("disabled",true);
$("#dropdown13").attr("disabled",true);
}