一:JavaScript原生的法门
后天有一id=test的下拉框,怎么获得当选的不行值吗?
对于以下select标签,获取当前挑选的值得情势如下:
金玉满堂 select中钦点option选中触发事件
大家在应用下拉列表框select时,须要对中选的<option>选项触发事件,其实<option>本人没有接触事件措施,大家只有在select里的onchange方法里接触。
想添加2个option的接触事件,在option中添加onclick
点来点去就是不会接触事件
又在select中添加onclick 那下可好了,没选option呢就接触了
百度来的说option没有接触事件,供给在select中加onchange事件,纵然笔者早已处理过类似的题材,用过就忘是否猪脑子….
本次记住了吗应该
当大家触发select的双击事件时,用ondblclick方法。
当大家要博取select的入选事件时,用document.all[‘name’].value来获取,其中name是select的名称。
假设大家要获得select的百分百的值就用2个for循环来实现。代码如下:
var vi = document.all[‘list’].length;
for(var i=0;i<vi;i++){
document.form2.list(i).value; //form2是<form>的名称
}
<select id="pid" onchange="gradeChange()">
<option grade="1" value="a">选项一</a>
<option grade="2" value="b">选项二</a>
</select>
<script type="text/JavaScript">
function gradeChange(){
var objS = document.getElementById("pid");
var grade = objS.options[objS.selectedIndex].grade;
alert(grade);
}
</script>
1
2
3
4
5
6
7
8
9
10
|
< select name="myselect" id="myselect">
< option value="opt1">选项1</ option >
< option value="opt2">选项2</ option >
< option value="opt3">选项3</ option >
</ select >
$("#myselect").change(function(){
var opt=$("#myselect").val();
...
});
|
Javascript得到select下拉框选中的的值
今后有一id=test的下拉框,怎么得到当选的可怜值吗?
分别使用javascript原生的点子和jquery方法
<select id=”test” name=””>
<option value=”1″>text1</option>
<option value=”2″>text2</option>
js获取select标签选中的值,jquery及原生js获取select下拉框选中的值示例。 </select>
code:
一:javascript原生的法门
1:拿到select对象: var myselect=document.getElementById(“test”);
2:获得当选项的目录:var index=myselect.selectedIndex ; //
selectedIndex代表的是你所选中项的index
3:获得当选项options的value: myselect.options[index]澳门葡京 ,.value;
4:获得当选项options的text: myselect.options[index].text;
二:jquery方法(前提是一度加载了jquery库)
1:var options=$(“#test option:selected”); //获取选中的项
2:alert(options.val()); //获得当选项的值
3:alert(options.text()); //获得当选项的公文
转载:
1:拿到select对象: var myselect=document.getElementById(“test”);
分级选择javascript原生的不二法门和jquery方法
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
2:获得当选项的目录:var index=myselect.selectedIndex ; //
selectedIndex代表的是你所选中项的index
复制代码 代码如下:
code:
一:javascript原生的艺术
1:拿到select对象: var myselect=document.getElementById(“test”);
2:得到当选项的目录:var index=myselect.selectedIndex ; //
selectedIndex代表的是你所选中项的index
3:获得当选项options的value: myselect.options[index].value;
4:获得当选项options的text: myselect.options[index].text;
二:jquery方法(前提是早就加载了jquery库)
3:获得当选项options的value: myselect.options[index].value;
<select id=”test” name=””>
<option value=”1″>text1</option>
<option value=”2″>text2</option>
</select>
1:var options=$(“#test option:selected”); //获取选中的项
4:获得当选项options的text: myselect.options[index].text;
一:javascript原生的艺术
2:alert(options.val()); //得到当选项的值
二:jQuery方法(前提是已经加载了jquery库)
1:拿到select对象: var myselect=document.getElementById(“test”);
3:alert(options.text()); //得到当选项的文书
1:var options=$(“#test option:selected”); //获取选中的项
2:获得当选项的目录:var index=myselect.selectedIndex ; //
selectedIndex代表的是您所选中项的index
2:alert(options.val()); //获得当选项的值
3:获得当选项options的value: myselect.options[index].value;
3:alert(options.text()); //得到当选项的公文
4:得到当选项options的text: myselect.options[index].text;
二:jquery方法(前提是一度加载了jquery库)
1:var options=$(“#test option:selected”); //获取选中的项
2:alert(options.val()); //得到当选项的值
3:alert(options.text()); //获得当选项的文件
分别使用javascript原生的办法和jquery方法 复制代码 代码如下: select
id=”test” name=”” optio…