㈠ 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>