js中怎么获得本页下拉菜单选定的值

JavaScript013

js中怎么获得本页下拉菜单选定的值,第1张

首先设置下拉列表控件的id属性

<select id="test" name="">

<option value="1">text1</option>

<option value="2">text2</option>

</select>

1:拿到select对象: var myselect=document.getElementById("test")

2:拿到选中项的索引:var index=myselect.selectedIndex// selectedIndex代表的是你所选中项的index。

3:拿到选中项options的value: myselect.options[index].value

4:拿到选中项options的text: myselect.options[index].text

另外还有jquery的方法(前提是已经加载了jquery库):

1:var options=$("#test option:selected") //获取选中的项

2:alert(options.val()) //拿到选中项的值

3:alert(options.text()) //拿到选中项的文本

下拉控件:<select id="selectId"></select>

如果你要获取的是动态生成所有的值,用如下方法:

var obj = document.getElementById('selectId')

var options = obj.options

for(var i=0,len=options.lengthi<leni++){

    var opt = options[i]

    alert(opt.value + '----' + opt.text)

}

如果你只要获取当前选择的值:

document.getElementById("selectId").value

<script language="javascript">

function getselectvalue()

{

var rtl=document.getElementById("rtl")

alert(rtl.options.(rtl.selectedIndex).value)

}

</script>

以楼上的数据来用

<select name="sel" id="rtl" >

<option>菜单一</option>

<option>菜单二</option>

<option>菜单三</option>

</select>

<input type="button" onclick="getselectvalue()" value="弹出下拉列表的值" />

加分咯!很辛苦的