两个select下拉框 实现根据s1的下拉选项控制s2可不可选
如下代码:
html:
<td><select id='s1' onchange='s1OnChange(this);'><option>可选</option><option>不可选</option></select></td>
<td><select id='s2'><option>No</option><option>Yes</option></select></td>js:
function s1OnChange(itemObj) {
    var selectIndex = itemObj.getAttribute("selectedIndex");
    if (selectIndex == 0)  //可选
    {
        document.getElementById("s2").setAttribute("disabled", "true");
    }
    else
    {
        document.getElementById("s2").setAttribute("disabled", "false");
    }
}
运行调试看到:selectIndex 取到的是null,document.getElementById("s2").setAttribute("disabled", "false");
找不到对象,菜鸟求指导

解决方案 »

  1.   

     itemObj.selectedIndex; 《= itemObj.getAttribute("selectedIndex");
      

  2.   

    这个确实如此,为什么用getAttribute不可以?还有下面document.getElementById("s2")报缺少对象是什么原因呢?
      

  3.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript" src="jquery-1.6.2.js"></script>
    <script type="text/javascript">
    function s1OnChange(itemObj) {
        var selectIndex = itemObj.getAttribute("selectedIndex");
        if (selectIndex == 0)  //可选
        {
            document.getElementById("s2").setAttribute("disabled", false);
        }
        else
        {
            document.getElementById("s2").setAttribute("disabled", true);
        }
    }</script>
    </head>
    <body>
    <td><select id='s1' onchange='s1OnChange(this);'><option>可选</option><option>不可选</option></select></td>
    <td><select id='s2'><option>No</option><option>Yes</option></select></td>
    </body>
    </html>
      

  4.   

    这个可以、、、我写的在html里调js为啥就不行呢
      

  5.   

    我把这个function定义在一个a.js文件中,然后html中<script type="text/javascript" src="a.js"></script>,就不可以,为啥???
      

  6.   

    是不是缓存
    改成这样<script type="text/javascript" src="a.js?1"></script>测试一下 原来的方法 火狐下有问题 
    改成这样 应高没问题了
    function s1OnChange() {
        var selectIndex = document.getElementById("s1").value;
    alert(selectIndex=="0");
        if (selectIndex == "0")  //可选
        {
            document.getElementById("s2").removeAttribute("disabled");
        }
        else
        {
            document.getElementById("s2").setAttribute("disabled",true);
        }
    }
      

  7.   

     document.getElementById("s2").setAttribute("disabled", true);