如何使用 JQUERY 在更改事件上禁用下拉列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/639900/
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
How to make a dropdownlist disabled on change event using JQUERY?
提问by Shiva
$(document).ready(function() {
$('#<%=ddlContinents.ClientID %>').change(function() {
var element = $(this);
var totalLength = element.children().length;
if ($(this).disabled == false) { $(this).disabled = true; }
});
});
What I am trying to do is fire off the change event of the dropdownlist
and on change making this dropdownlist
disabled. The code is firing and everything, but it does not disable the dropdownlist
.
我想要做的是触发 和 的更改事件,dropdownlist
使此dropdownlist
禁用。代码正在触发和一切,但它不会禁用dropdownlist
.
This portion of the code is not working:
这部分代码不起作用:
if ($(this).disabled == false) { $(this).disabled = true; } });
回答by Erik L
You should use .prop()
for jQuery 1.6+ or .attr()
for earlier versions of jQuery:
您应该使用.prop()
jQuery 1.6+ 或.attr()
更早版本的 jQuery:
> jQuery 1.6:
> jQuery 1.6:
$(document).ready(function() {
$('#<%=ddlContinents.ClientID %>').change(function() {
var element = $(this);
var totalLength = element.children().length;
if (!$(this).prop("disabled")) {
$(this).prop("disabled", true);
}
});
});
< jQuery 1.6:
< jQuery 1.6:
$(document).ready(function() {
$('#<%=ddlContinents.ClientID %>').change(function() {
var element = $(this);
var totalLength = element.children().length;
if (!$(this).attr("disabled")) {
$(this).attr("disabled", "disabled");
}
});
});
回答by Paolo Bergantino
if (!$(this).attr("disabled")) { $(this).attr("disabled","disabled"); }
If you want to enable it later on, you gotta do:
如果您想稍后启用它,您必须执行以下操作:
$(this).removeAttr("disabled");
回答by chitra pannirselvam
I know this post is old..This might help if anyone stuck with disabling dropdown on dropdown chnage function
我知道这篇文章很旧..如果有人坚持禁用下拉更改功能的下拉菜单,这可能会有所帮助
if ($(this).attr('disabled', false))
{ $(this).attr('disabled', true);
}