最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How can change disabled style for select box with javascript - Stack Overflow

programmeradmin3浏览0评论

I want to change the second's select box disabled style when the first select box is changed but I can't. Please help me.

<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>

I want to change the second's select box disabled style when the first select box is changed but I can't. Please help me.

<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>
Share Improve this question edited Oct 25, 2013 at 13:04 cfi 11.3k9 gold badges59 silver badges106 bronze badges asked Oct 25, 2013 at 12:46 user2729868user2729868 171 gold badge1 silver badge5 bronze badges 2
  • @Donte'Trumble You shouldn't need a Fiddle for something as simple as this ;) – Niet the Dark Absol Commented Oct 25, 2013 at 12:53
  • Yeah, that's why I deleted it. ;) – dowomenfart Commented Oct 25, 2013 at 12:54
Add a ment  | 

3 Answers 3

Reset to default 3

disabled is a property of the element, NOT its style collection.

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

It is also important to note that 1 and 2 are NOT valid IDs in HTML older than HTML5, which means older browser may have severe issues with it (such as not recognising it as an ID, not finding it with getElementById, not styling it, etc.) I suggest giving meaningful IDs, even if it's just select1 and select2, that helps reduce the chance of accidentally duplicating IDs.

The "disabled" part is an attribute of the select element, not a CSS property.

Try this instead :

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

disabled is an attribute, not style property. That should do:

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

评论列表(0)

  1. 暂无评论