導航:首頁 > 編程語言 > 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

友情鏈接