如何获取select里面的option的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取select里面的option的值相关的知识,希望对你有一定的参考价值。

参考技术A jQuery获取Select元素,并设置的 Text和Value:
$("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
$("#select_id ").val(4); // 设置Select的Value值为4的项选中
$("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中
jQuery添加/删除Select元素的Option项:
$("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)
$("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)
$("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
$("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)
$("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option
$("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option
三级分类 <select name="thirdLevel" id="thirdLevel"
onchange="getFourthLevel()">
<option value="0" id="thirdOption">
请选择三级分类
</option>
</select>
</div>
四级分类:
<select name="fourthLevelId" id="fourthLevelId">
<option value="0" id="fourthOption">
请选择四级分类
</option>
</select>
</div>
if($("#thirdLevel").val()!=0)
$("#thirdLevel option[value!=0]").remove();

if($("#fourthLevelId").val()!=0)
$("#fourthLevelId option[value!=0]").remove();
//这个表示:假如希望当选择选择第三类时:如果第四类中有数据则删除,如果没有数据第四类的商品中的为默认值。
获取Select :
获取select 选中的 text :
$("#ddlRegType").find("option:selected").text();
获取select选中的 value:
$("#ddlRegType ").val();
获取select选中的索引:
$("#ddlRegType ").get(0).selectedIndex;
设置select:
设置select 选中的索引:
$("#ddlRegType ").get(0).selectedIndex=index;//index为索引值本回答被提问者和网友采纳

js如何获取到select的option值?

1、首先输入下方中的代码:

var obj = document.getElementByIdx_x(”testSelect”); //定位id

var index = obj.selectedIndex; // 选中索引

2、然后继续输入下方中的代码:

var text = obj.options[index].text; // 选中文本

var value = obj.options[index].value; // 选中值

3、最后运行了就可以获取到select的option值了,效果图:

参考技术A

1、获得选项option的值

var obj=document.getElementById('mySelect');
var items=document.getElementById("sect").options;//获取select的所有option
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].value;

2、获得选项option的文本

var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].text;

2、获得选项option的value值

var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].value;

参考技术B document.getElementById(“select的ID”).value

参考技术C 代码如下:
javascript:
txtcoupon.onchange = function ()
couponprice.innerHTML = this.children[this.selectedIndex].getAttribute("price");

jquery:
$("#txtcoupon").change (function ()
$("#couponprice").text($(this).children(":selected").attr("price"));
);
参考技术D </script回答:document.getElementById(sect).value -----这是获得选中的值document.getElementById(sect).options------这是获得select中所有的值,是个数组追问既然是数组,那怎么遍历出来啊, 我这个是专门要获取用户在前台选择什么哪个,获取到我要去数据库里添加,这个就是个字段 你懂? 写完整点回答那你就用第一个叫好啦document.getElementById(sect).value ,这个就获得的是用户选中的值追问我要获得option中间的文本,这下理解了吧?回答那就应该是var selectIndex = document.getElementById(sect).selectedIndex;//获得是第几个被选中了var selectText = document.getElementById(sect).options[selectIndex].text //获得被选中的项目的文本但我觉得一般都不会这样用的,你既然后台要的是文字,把value字段搞成文字,如“第一名”这样不就好了function foo()var noties = document.frm.olala.value;var sel = document.frm.sele.value; // var sel = document.getElementById('sect').value;var sels = document.getElementsByName('sele')[0]; //如果该sele是所有同名控件中的第一个的话.追问第二个 sels 出来的是数组, 怎么遍历,详细点, 我上面的那个只是随便写了一个例子而已,其实我现在要做的是, 有一个增加的页面, 有用户名、手机号 还有这个select的标记,我要用js在用户点击提交的时候 我获取到他在前台点击的每个参数值,然后插入到数据库里,你理解嘛?写个详细的小例子,让我明白了就行,谢谢回答如果你的SELECT是单选的,不用遍历OPTIONS集合,直接通过SELECT的VALUE属性即可取到用户选择的值.如果是多选,就需要遍历.function foo()var noties = document.frm.olala.value;var sel = document.frm.sele.value; // var sel = document.getElementById('sect').value;alert(document.getElementById('sect').options(i).selected);追问我不想弹框,我只想传值, 其实我想实现的目的 上次的追问已经告诉你了,我现在给你截个图,看看这个该怎么实现啊,上面是可查询可添加的, 选框和标签就那么几个, 你要有是点时间就把这个整个的html静态给我写个吧剩下的bug我来修改,我现在其实就差这个seect的option值了,不知道怎么获取到。 还有一个问,就是我页面上方 可查询、可添加,我想在它的下面显示添加后的信息列表或者是查询后的信息列表, 给你节截个图回答很难和你沟通,你说这么多都不知道说的是什么意思,你要提交到后台处理的东西为什么还要用JS来取值? 即然你看不懂我写的JS,那你就等别人来帮你解答好了. 我实在是没时间帮你写完一个页.本回答被提问者采纳

以上是关于如何获取select里面的option的值的主要内容,如果未能解决你的问题,请参考以下文章

jquery如何获取一个select里面的所有option的值呢?

jQuery怎么获取Select的option个数

在js中怎样获取option里的值,让他回显,谢谢

js如何获取到select的option值?

如何获取select 选中的option的值

jQuery怎么获取Select的option个数