您现在的位置是:首页 > 电脑技术查询 > web开发

form里面怎么获取下拉框的值

编辑:chaxungu时间:2022-10-02 23:17:08分类:web开发

form里面如何获取下拉框的值?
<form name="aaa">
<select name="sarea" /);
</form>
按钮onclick:
alert(this.sarea.options[this.sarea.selectedIndex].value,'','_self')
window.location = "/a_"+aaa.sarea.options[aaa.sarea.selectedIndex].value+"_"+aaa.sclassids.options[searchnavform.sclassids.selectedIndex].value+".htm";
} 到底是哪里出错了?为什么加了form就不行了呢?

------解决方案--------------------
你用document.getElementsByName("sarea")[0] or document.getElementById("sareaId") 来取的对像------解决方案--------------------
1给form加个id
<form id="aaa" name="aaa">
<select name="sarea"/>
</form>
alert(aaa.sarea.options[aaa.sarea.selectedIndex].value, ' ', '_self ')可取到

2使用document.getElementsByName
var select = document.getElementsByName("sarea")[0];
alert(select.options[select.selectedIndex].value,'','_self');
------解决方案--------------------
form 要的是name,id没用