导航:首页 > 编程语言 > js中select选中的值

js中select选中的值

发布时间:2021-03-31 00:32:45

js获取select option获取选中的值多选

|
  1. 纯JS

  2. vare=document.getElementById("form-field-select-4");
  3. alert(getSelectValues(e));
  4. //;回
  5. //selectisanHTMLselectelement;
  6. functiongetSelectValues(select){;
  7. varresult=[];
  8. varoptions=select&&select.options;
  9. varopt;
  10. for(vari=0,iLen=options.length;i<iLen;i++){
  11. opt=options[i];
  12. if(opt.selected){
  13. result.push(opt.value|答|opt.text);
  14. }
  15. }
  16. returnresult;
  17. }

㈡ 怎样用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>

阅读全文

与js中select选中的值相关的资料

热点内容
网络中常用的传输介质 浏览:518
文件如何使用 浏览:322
同步推密码找回 浏览:865
乐高怎么才能用电脑编程序 浏览:65
本机qq文件为什么找不到 浏览:264
安卓qq空间免升级 浏览:490
linux如何删除模块驱动程序 浏览:193
at89c51c程序 浏览:329
怎么创建word大纲文件 浏览:622
袅袅朗诵文件生成器 浏览:626
1054件文件是多少gb 浏览:371
高州禁养区内能养猪多少头的文件 浏览:927
win8ico文件 浏览:949
仁和数控怎么编程 浏览:381
项目文件夹图片 浏览:87
怎么在东芝电视安装app 浏览:954
plc显示数字怎么编程 浏览:439
如何辨别假网站 浏览:711
宽带用别人的账号密码 浏览:556
新app如何占有市场 浏览:42

友情链接