这是我写的一段关于两个下拉菜单联动的JS代码,targForm.selName和targForm.selCompany是<select id="selName" onchange="CreateClassID(this.form)"></select> 和<select id="selCompany"></select> ,我想问下,如果有一个 <asp:HiddenField ID="HDFThirdComponentID" runat="server" Value='<%# Bind("ThirdComponentID") %>' />,以上这些都在一个GridView控件中。
我想加载时,<select id="selName" onchange="CreateClassID(this.form)"></select>加载完所有选项后,显示的当前选项是select中的value等于HiddenField中Value值的那一项,这样的话,JS中应该怎么写?
function CreateRootID(targForm) {
    var targ = targForm.selName;
    for (i = 0; i < ThirdComponent.length; i++) {        targ.options[i] = new Option(ThirdComponent[i][1], ThirdComponent[i][0]);
    }
}
function CreateClassID(targForm) {//生成二级分类 targForm为目标表单
    var parent = targForm.selName;
    var subfield = targForm.selCompany;
    var classid;
    classid = parent.options[parent.selectedIndex].value;
    subfield.length = 0;
    subfield.options[subfield.length] = new Option('二级分类', '-1');
    for (i = 0; i < ThirdCompany.length; i++) {
        if (ThirdCompany[i][2] == classid) {
            subfield.options[subfield.length] = new Option(ThirdCompany[i][1], ThirdCompany[i][0])
        }
    } 
}

解决方案 »

  1.   

    遍历 所有的 option 直到  option 的 value是你指定的value
      

  2.   


    var choose = o.parentNode.getElementsByTagName("select");
    alert(choose[0].options[choose[0].selectedIndex].value);
      

  3.   

    随便写了个,LZ修改下就OK<html>
    <head><title></title>
    <script language="javascript">
    function $(s)
    {
    return document.getElementById(s);
    }
    function InitSel(id)
    {
    var i;
    for(i=0;i<$("selone").options.length;i++)
    {
    if($('selone').options[i].value == id)
    {
    $('selone').options[i].selected = true;
    }
    }
    }
    </script>
    </head>
    <body onload="InitSel('b');">
    <select id="selone">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    </select>
    </body>
    </html>