如何让 select的那个请选择不被选中.获取选中的value值和html

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何让 select的那个请选择不被选中.获取选中的value值和html相关的知识,希望对你有一定的参考价值。

获取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);
参考技术A 啥意思,完全没懂

Vue+Element+Select获取选中的对象

技术图片         技术图片     

 

案例演示:获取select当前选中的所有内容

 

技术图片

 

1 <el-select v-model="value8" filterable placeholder="请选择" value-key="id" @change="currentSel">
2    <el-option v-for="item in options" :key="item.id" :label="item.label" :value="item"></el-option>
3 </el-select>

 

 1       options: [
 2         {
 3           value: "选项1",
 4           id: 1,
 5           code: "xuanxiang1",
 6           label: "黄金糕"
 7         },
 8         {
 9           code: "xuanxiang2",
10           id: 2,
11           value: "选项2",
12           label: "双皮奶"
13         },
14         {
15           id: 3,
16           value: "选项3",
17           code: "xuanxiang3",
18           label: "蚵仔煎"
19         },
20         {
21           value: "选项4",
22           id: 4,
23           code: "xuanxiang4",
24           label: "龙须面"
25         },
26         {
27           value: "选项5",
28           label: "北京烤鸭",
29           id: 5,
30           code: "xuanxiang5"
31         }
32       ],

 

1 currentSel(selVal) {
2       this.code = selVal.code;
3       this.name = selVal.label;
4       console.log("选择的name为:" + this.name, "选择的code为:" + this.code);
5       console.log(selVal);
6     },

 

若有不明白请加群号:复制 695182980 ,也可扫码,希望能帮助到大家。

以上是关于如何让 select的那个请选择不被选中.获取选中的value值和html的主要内容,如果未能解决你的问题,请参考以下文章

css3 - css中如何让第一个和最后一个不被选中?

我想使用juqery让id为selectd的那个下拉框选中,请问jquery怎么做?

android 中如何获取radiogroup 中那个radiobutton被选择

js 设定下拉框的值默认被选中,下拉框做条件查询时,实现分页的时候带参数传值,下拉框默认被选中,求解!

如何获取和设置HTML文档中select元素的选中项

怎么清除select已选择的内容