
var op=document.createElement("option") //新建option
op.setAttribute("value",0) // 设置value
op.appendChild(document.createTextNode("请选择---")) //text
sel.appendChild(op) // 为select添加option
这样就可以给select添加option了
获取select元素的选中项:在HTML页面中,有时候会获取当前select元素中所选中的那个值和显示值。下面是一个例子:
<tr>
<th scope="row" width="15%" nowrap >*目标字段</th>
<td><select name="idMbzd" style="width:25%" onchang=”on_idmbzd_change()”>
<option value=”1”>eg1</option>
<option value=”2”>eg2</option>
<option value=”3”>eg2</option>
</select>
</td>
</tr>
<script>
function on_idmbzd_change(){
var sel_obj = document.getElementByIdx("idMbzd ")
var index = sel_obj.selectedIndex
alert(sel_obj.options[index].value)
alert(sel_obj.options[index].text)
}
</script>
在这里,关键用到select对象的selectedIndex属性。表示被选中那个元素的索引,从0开始。
当然也可以遍历select元素的所有值。如下:
<script>
var len = sel_obj.options.length
for(i = 0 i<len i++){
var value = sel_obj.options[i].value
var view_value = sel_obj.options[i].text
}
</script>
也多说一下,可以动态添加他的元素,如下:
<script>
var voption = document.createElement("OPTION")
voption.value = "4"
voption.text. = "eg4"
sel_obj.add(voption)
</script>
设置select元素的选中项:
方法有两种。
第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果。
<select id = "sel" >
<option value = "1" >1</ option >
<option value = "2" selected = "selected" >2</ option >
<option value = "3" >3</ option >
</ select >
第二种为通过前端js来控制选中的项:
<script type = "text/javascript" >
function change(){
document.getElementById("sel")[2].selected=true
}
</ script >
<select id = "sel" >
<option value = "1" >1</ option >
<option value = "2" >2</ option >
<option value = "3" >3</ option >
</ select >
<input type = "button" value = "修改" onclick = "change()" />
获取<select>标签选中项文本的js代码为:
var val = document.all.Item.options[document.all.Item.selectedIndex].text
var i=document.getElementById( ‘sel‘ ).options[document.getElementById( ‘sel‘).selectedIndex].value
一些其它 *** 作<select>标签的技巧如下:
1)动态创建select
function createSelect(){
var mySelect = document.createElement( "select" )
mySelect.id = "mySelect"
document.body.appendChild(mySelect)
}
2)添加选项option
function addOption(){
//根据id查找对象,
var obj=document.getElementById( ‘mySelect‘ )
//添加一个选项
obj.add( new Option( "文本" , "值" ))
}
3)删除所有选项option
function removeAll(){
var obj=document.getElementById( ‘mySelect‘ )
obj.options.length=0
}
4)删除一个选项option
function removeOne(){
var obj=document.getElementById( ‘mySelect‘ )
//index,要删除选项的序号,这里取当前选中选项的序号
var index=obj.selectedIndex
obj.options.remove(index)
}
5)获得选项option的值
var obj=document.getElementById( ‘mySelect‘ )
var index=obj.selectedIndex //序号,取当前选中选项的序号
var val = obj.options[index].value
6)获得选项option的文本
var obj=document.getElementById( ‘mySelect‘ )
var index=obj.selectedIndex //序号,取当前选中选项的序号
var val = obj.options[index].text
7)修改选项option
var obj=document.getElementById( ‘mySelect‘ )
var index=obj.selectedIndex //序号,取当前选中选项的序号
var val = obj.options[index]= new Option( "新文本" , "新值" )
8)删除select
function removeSelect(){
var mySelect = document.getElementById( "mySelect" )
mySelect.parentNode.removeChild(mySelect)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)