
在HTML中,select控件的值等于其当前选中的option的值,所以:
$("select")val(); // 可以获取select当前的值
如果想获取当前select下option的所有的值,则:
var selValue = []; // 定义一个空数组用于接收select下option所有的值
var options = $("select")find("option"); // select下所有的option
for (var i=0; i<optionslength; i++) {
selValuepush(optionseq(i)val()); // 将所有的值赋给数组
}
假设一个select的id="select1"
那么这个select所有option的集合是:var options = documentgetElementById("select1")options;即可获取
既然可以获取到option集合,那每一个option的value就不言而喻了。
比如select中第一个选项的value为 var option_value1 = documentgetElementById("select1")options[0]value;
交换两个option的值(和名称)的方式也很简单。
比如这个select的有两个option,分别是<option value="1">1</option>和<option value="2">2</option>,现在交换两个option的位置(即交换值(名称))。
var option_temp = getElementById("select1")options[0];
documentgetElementById("select1")options[0]value = documentgetElementById("select1")options[1]value;
documentgetElementById("select1")options[0]text = documentgetElementById("select1")options[1]text;
documentgetElementById("select1")options[1]value= option_tempvalue;
documentgetElementById("select1")options[1]text = option_temptext;
通过value获得options的index编号的思路:获取所有option选项数组→循环判断value属性→取得满足要求的option的index值。如果获取options的index编号的目的是为了将其设置为选中项,那么可以有更简单的方式——直接将select对象的value属性值设置为需要选中项的value值即可。实例演示如下:
1、HTML结构
<select id="test"><option value="0">option-0</option>
<option value="1">option-1</option>
<option value="2">option-2</option>
<option value="3">option-3</option>
<select><br>
选中项value:<input type="text" id="val"><input type="button" value="确定" onclick="fun()">
2、javascript代码
function fun(){var val = documentgetElementById("val")value;
var select = documentgetElementById("test"); // 获取select对象
selectvalue = val; // 设置选中项
// 下面获取目标value值的option的index值
index = 0;
for(i=0;i<selectlength;i++){
if(select[i]value == val){
index = i;
break;
}
}
alert(index);
}
3、效果演示
1documentformNameselectoptionslength2documentformNameselectoptions[documentformNameselectselectedIndex]text//终级用户如果你要用asp调用,就加一个隐藏域,比如shu,在这里加一句<select name=select onchang=documentformNameshuvalue=documentformNameselectoptionslength要更详细的值,道理一样,或者你可以做一个javascript
以上就是关于“jquery”怎么获取“ select option ”的值全部的内容,包括:“jquery”怎么获取“ select option ”的值、怎么样交换select中两个option的value值,怎么获得select里边的所有的option的value的。、用js,如何通过value获得options的index编号等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)