如何使用 javascript 更改选择框的禁用样式

How can change disabled style for select box with javascript

本文关键字:样式 选择 何使用 javascript      更新时间:2023-09-26
我想

在更改第一个选择框时更改第二个选择框禁用样式,但我不能。请帮助我。

<html>
    <body>
        <select onchange="a()" id="1">
            <option>Choose</option>
            <option>1</option>
            <option>2</option>
        </select>
        <select id="2" disabled="true">
            <option>one</option>
            <option>two</option>
        </select>
        <script>
            function a(){
                if(document.getElementById('1').value!="Choose"){
                    document.getElementById('2').style.background="yellow";
                    document.getElementById('2').style.disabled="false";
                }
            }
        </script>
   </body>
</html>

>disabled是元素的属性,而不是它的样式集合。

document.getElementById('2').disabled = false;

同样重要的是要注意,12不是早于 HTML5 的 HTML 中的有效 ID,这意味着较旧的浏览器可能存在严重的问题(例如无法将其识别为 ID、无法找到它getElementById、未设置样式等)我建议提供有意义的 ID,即使它只是select1select2,这有助于减少意外重复 ID 的机会。

"disabled"部分是 select 元素的属性,而不是 CSS 属性。

试试这个:

document.getElementById('2').disabled=false;

disabled是一个属性,而不是样式属性。这应该可以:

function a(){
    if(document.getElementById('1').value!="Choose"){
        document.getElementById('2').style.background = "yellow";
        document.getElementById('2').disabled = false;
    }
}