javascript 如何使用 jQuery 在 html 中启用或禁用选择标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6838620/
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 do you enable or disable a select tag in html using jQuery?
提问by RetroCoder
How do you enable or disable a select tag after selecting an option from the first select tag in jQuery, but current version is not working.
从 jQuery 的第一个选择标签中选择一个选项后,如何启用或禁用选择标签,但当前版本不起作用。
$("select[name='selectALPHA']").change(function () {
if ($("select[name='selectBRAVO'"].attr('enabled') == false {
$("select[name='selectBRAVO']").attr('enabled', 'true');
}
else
{
$("select[name='selectBRAVO']").attr('enabled', 'false');
}
});
回答by Matt Ball
.attr()
expects a boolean.
.attr()
期望一个布尔值。
$("select[name='selectBRAVO']").attr('disabled', false);
The code also contains syntax errors. Fixes:
该代码还包含语法错误。修复:
var $bravo = $("select[name='selectBRAVO']");
$("select[name='selectALPHA']").change(function () {
$bravo.attr('enabled', !$bravo.attr('enabled'));
});
回答by RobG
You seem to want to toggle the value, in plain js:
您似乎想在纯 js 中切换值:
var select = document.getElementsByName('selectBRAVO')[0];
select.disabled = !select.disabled;
I'm sure you can figure out the jQuery equivalent.
我相信你可以找出 jQuery 等价物。
Edit
编辑
Here's one different to Matt's based on Reid's suggestion:
根据里德的建议,这是与马特的不同之处:
var select = $("select[name='selectBRAVO']")[0];
select.disabled = !select.disabled;
The old jQuery attrmethod mixed up HTML attributes and DOM properties rather badly. In the new version, attrdeals only with HTML attributes, which is better. However, in the vast majority of cases, what programmers really want is the DOM property. That can be accessed directly, there is no need to call attror getAttribute. Direct property access is (hugely) faster, less to type and easier to read.
旧的 jQuery attr方法相当糟糕地混合了 HTML 属性和 DOM 属性。在新版本中,attr只处理 HTML 属性,这样更好。然而,在绝大多数情况下,程序员真正想要的是 DOM 属性。可以直接访问,不需要调用attr或getAttribute。直接属性访问(极大地)更快,键入更少且更易于阅读。
回答by Pierre Henry
I'm not so very sure what you are trying to achieve here but I think that this can help you :
我不太确定您要在这里实现什么,但我认为这可以帮助您:
$("select[name='select1']").change(function () {
var jSelect2=$("select[name='select2']");
if($(this).val()){
jSelect2.removeAttr('disabled');
}else{
jSelect2.attr('disabled', 'disabled');
}
});
See working example here : JSFiddle
请参阅此处的工作示例:JSFiddle
回答by dpp
We all know that a select element has an attribute of disabled
:
我们都知道一个 select 元素有一个属性disabled
:
<select disabled="disabled">
</select>
And to manipulate the value of an attribute using jQuery, As an example on http://api.jquery.com/attr/#attr2:
并使用 jQuery 操作属性的值,以http://api.jquery.com/attr/#attr2为例:
$("button:gt(1)").attr("disabled","disabled");
You can do the same in select element:
您可以在 select 元素中执行相同的操作:
$("select[name='selectALPHA']").change(function () {
if ($("select[name='selectBRAVO'"].attr('disabled') == 'disabled'{
$("select[name='selectBRAVO']").removeAttr('disabled');
}
else
{
$("select[name='selectBRAVO']").attr('disabled', 'disabled');
}
});