js给下拉框赋值(多个同类下拉框赋值)

js给下拉框赋值(多个同类下拉框赋值)

//根据指定的 Name 值获取相应被选中的 radio 值function GetRadioValue(_radioName) { var radios = document.getElementsByName(_radioName); for (var i = 0; i < radios.length; i ) { if (radios[i].checked) { return radios[i].value; }}}假设页面上有多个<input name=”radios” type=”radio” />标签,GetRadioValue(“radios”)能够获取被选中单选按钮的 value 属性值。同理,获取所有复选框的值只需稍稍调整代码便是。//示例代码var checks = document.getElementsByName(“checks”);var selected_datas = new Array();for (var i = 0; i < checks.length; i ) { if (checks[i].checked) { selected_datas.push(checks[i].value); }}思路是在原代码基础上多加一个数组用于存储复选框中的值。【获取下拉框选中的值】假设 select 元素的结构如下:<select id=”select1″> <option value=”1″ selected=”selected”>value1</option> <option value=”2″>value2</option> <option value=”3″>value3</option></select>

select 元素的 options 属性可以获取到其下所有的 option 元素,然后再通过选中项的索引值获取相应 option 元素的值:

var select1 = document.getElementById(“select1”);//选中项的索引 select1.selectedIndex//显示文本 select1.options[0].text//值 select1.options[0].value

发表评论

登录后才能评论