首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

select上拉框的有关问题

2013-01-23 
select下拉框的问题两个select下拉框 实现根据s1的下拉选项控制s2可不可选如下代码:html:tdselect id

select下拉框的问题
两个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");
找不到对象,菜鸟求指导
[解决办法]
<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>

热点排行