页面加载时,因为没有任何选中项,所以 secondElementselectedIndex 值是 -1,secondElement
[-1]为undefined,所以alert()会d出 undefined 或 一个空的对话框;
如果你用鼠标选中了“选项9”,secondElementselectedIndex 值是 0,secondElement
[0]为"<option value="选项9">选项9</option>",所以alert()会d出 [object HTMLOptionElement]
把代码做一些修改:
<option value="选项9">选项9</option> 修改为
<option value="选项9" selected>选项9</option>
alert(secondElement[secondElementselectedIndex]); 修改为 alert(secondElement[secondElementselectedIndex]value);
javascript获取select下option个数常用代码有:
obj = documentgetElementById(select_id);objlength;
objoptionslength;
objchildrenlength;
实例演示:
1、HTML结构
<select id="test"><option value="1">option-1</option>
<option value="2">option-2</option>
<option value="3">option-3</option>
<option value="4">option-4</option>
</select><br>
<input type='button' value='获取下拉列表项数目' onclick="fun()"/>
2、javascript代码
function fun(){obj = documentgetElementById("test");
alert(objlength); // 方法1
alert(objoptionslength); // 方法2
alert(objchildrenlength); // 方法3
}
3、演示效果(点击查看动态效果)
单选下拉列表框对象的value属性值就是选中项的value值,因此只需用如下代码即可 var selected_val = documentgetElementById(select_id)value;并且,通过 *** 作select下的option也可以得到被选项的value值,方法为: var sel = documentgetElem怎样用js取得select下拉列表框内选中的option的val
每一个选择框select都有一个options的列表对象。
<select id="subject" name="subject">
<option value="one">文本值1</option>
<option value="two">文本值2</option>
<option value="three">文本值3</option>
</select>
使用:options[2]value,这样可以得到值“three”;使用options[2]text,得到“文本值3”
appendChild() 把新的子节点添加到指定节点。(所有元素对象都有此方法)
add() 向下拉列表添加一个选项。 (只专属于 select 对象的方法)
因为select 子集只能是 option 所以建议使用 add 没有在select 中用 appendChild 添加过子集,你可以尝试下!
以上就是关于javascript获取select标签下的option的value求解全部的内容,包括:javascript获取select标签下的option的value求解、js怎么得到option个数、怎样用js取得select下拉列表框内选中的option的val等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)