㈠ js获取select option获取选中的值多选
|纯JS
㈡ 怎样用js取得select下拉列表框内选中的option的value值呢
单选下拉列表框对象的value属性值就是选中项的value值,因此只需用如下代码即可
var selected_val = document.getElementById(select_id).value;
并且,通过操作select下的option也可以得到被选项的value值,方法为:
var sel = document.getElementById(select_id);
var selected_val = sel.options[sel.selectedIndex].value;
㈢ javaScript怎样获取select标签当前选择的值呢
对于以下select标签,获取当前选择的值得方式如下:
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</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;
㈣ JavaScript中如何获取下拉框中选中的值
第一种:document.getElementById('se').value;
第二种:document.getElementsByName('sel')[0].value;
第三种不推荐.是用tagName.以上两种足够用了.
㈤ javascript 中怎么获取select选中多项的值
var SeleObj = document.getElementById("selectID");
var tmpSelectValues="";
for (var i = 0;i<SeleObj.length; i++) {
if (SeleObj.options[i].selected) {
//选中项的值 SeleObj.options[i].value;
tmpSelectValues+=SeleObj.options[i].value+",";
}
}
要到后台使用的话,你可以tmpSelectValues赋值于一个隐藏控件,然后提交到后台
㈥ 怎么在js中获得select标签被选中的值
JS 控制select选中项,代码如下:
<html>
<script type="text/javascript">
var selectedValue = '<%= request.getAttribute("line")%>';
function changeSelected(){
jsSelectItemByValue(document.getElementById("mySelect"),selectedValue);
}
function jsSelectItemByValue(objSelect,objItemText) {
for(var i=0;i<objSelect.options.length;i++) {
if(objSelect.options[i].value == objItemText) {
objSelect.options[i].selected = true;
break;
}
}
}
</script>
<body onload="changeSelected()">
<select id="mySelect" name="mySelect">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</body>
</html>
㈦ 如何使用js获取select中选中的option的value值
现在有一id=test的下拉框,怎么拿到选中的那个值呢?
分别使用javascript原生的方法和jquery方法
<selectid="test"name="">
<optionvalue="1">text1</option>
<optionvalue="2">text2</option>
</select>
code:
一:javascript原生的方法
1:拿到select对象:varmyselect=document.getElementById("test");
2:拿到选中项的索引:varindex=myselect.selectedIndex;//selectedIndex代表的是你所选中项的index
3:拿到选中项options的value:myselect.options[index].value;
4:拿到选中项options的text:myselect.options[index].text;
二:jquery方法(前提是已经加载了jquery库)
1:varoptions=$("#testoption:selected");//获取选中的项
2:alert(options.val());//拿到选中项的值
3:alert(options.text());//拿到选中项的文本
㈧ 中js想动态设置select选中的值怎么弄
可以使用javascript和jQuery两种实现方式
1:使用javascript实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select name="jumpMenu" id="jumpMenu" onChange="jumpMenu('parent',this,0)">
<option id="1" value="跳转URL">111</option> // 111 是显示给用户的信息
<option id="2" value="跳转URL">222</option>
<option id="3" value="跳转URL">333</option>
<option id="4" value="跳转URL">444</option>
<option id="5" value="跳转URL">555</option>
</select>
<script type="text/javascript">
function display(optionID){
var all_options = document.getElementById("jumpMenu").options;
for (i=0; i<all_options.length; i++){
if (all_options[i].id == optionID) // 根据option标签的ID来进行判断 测试的代码这里是两个等号
{
all_options[i].selected = true;
}
}
};
display("4");
</script>
</body>
</html>
2:使用jQuery实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select name="jumpMenu" id="jumpMenu" >
<option value="1">111</option> // 111 是显示给用户的信息
<option value="2">222</option>
<option value="3">333</option>
<option value="4">444</option>
<option value="5">555</option>
</select>
<script type="text/javascript" src="js/jquery1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
// $("#jumpMenu").val(要选中的option的value值即可);
$("#jumpMenu").val(4);
});
</script>
</body>
</html>