简体   繁体   English

如何使用jQuery在HTML中启用或禁用选择标签?

[英]How do you enable or disable a select tag in html using jQuery?

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');
    }
});

.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'));
});

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 attr method mixed up HTML attributes and DOM properties rather badly. 旧的jQuery attr方法将HTML属性和DOM属性混为一谈。 In the new version, attr deals only with HTML attributes, which is better. 在新版本中, attr只处理HTML属性,这更好。 However, in the vast majority of cases, what programmers really want is the DOM property. 但是,在大多数情况下,程序员真正想要的是DOM属性。 That can be accessed directly, there is no need to call attr or getAttribute . 可以直接访问,无需调用attrgetAttribute Direct property access is (hugely) faster, less to type and easier to read. 直接属性访问(非常快),类型更少且更易于阅读。

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');
    }
});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript禁用/启用HTML5视频标记 - How to disable/enable HTML5 Video tag using JavaScript 如何使用 javascript/jquery 在 select 下拉菜单中启用/禁用引导选项 - How to enable/disable bootstrap option in select dropdown using javascript/jquery 如何使用Javascript / jQuery隐藏/显示启用/禁用HTML元素? - How hide/show enable/disable HTML elements using Javascript/jQuery? 如何使用jQuery禁用/启用按钮? - How do I disable/enable buttons using jQuery? 如何禁用pageload上的Anchor(a)标签或(默认情况下禁用)并使用jquery或Javascript启用它? - How to disable Anchor(a ) tag on pageload or (by default disable) and enable it using jquery or Javascript? 动态启用/禁用选择标签 - Dynamically enable/disable select tag 在使用JavaScript或JQuery选择HTML中的选定项目之后,如何禁用项目? - How do I disable items after the selected item in an HTML select using JavaScript or JQuery? 如何使用Javascript禁用和启用HTML表? - How to disable and enable HTML table using Javascript? 如何在 java 字符串中启用作为 html 接收的禁用锚标签 - How to enable disable anchor tag received as html in java string 如何使用JavaScript检查是否存在html元标记? - How do you check if a html Meta tag exist using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM