javascript獲取select值的方法完整實例
本文實例講述了javascript獲取select值的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>chabaoo.cn javascript獲取select值</title>
</head>
<script>
//javascript獲取選中select值
/*
var obj = document.getElementById("testSelect"); //定位id
var index = obj.selectedIndex; // 選中索引
var text = obj.options[index].text; // 選中文本
var value = obj.options[index].value; // 選中值
*/
//jQuery獲取選中select值
/*
$('#testSelect option:selected').text();//選中的文本
$('#testSelect option:selected') .val();//選中的值
$("#testSelect ").get(0).selectedIndex;//索引
*/
window.onload=function() {
var oBtn=document.getElementById("oBtn1");
var oText=document.getElementById("oText1");
var oSelect=document.getElementById("select1");
var index=oSelect.selectedIndex ;
oBtn.onclick=function() {
alert(oSelect.options[index].text);
if(oText.value==""){
alert("請輸入內(nèi)容")
}else{
alert(oText.value+"----"+oSelect.options[oSelect.selectedIndex].text)
}
}
}
</script>
<body>
<input id="oText1" type="text" value=""/>
<input id="oBtn1" type="button" value="按鈕"/>
<select id="select1">
<option value="1">廣州</option>
<option value="2">上海</option>
<option value="3">北京</option>
</select>
</body>
</html>
運行結(jié)果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
javascript移出節(jié)點removeChild()使用介紹
這篇文章主要介紹了javascript節(jié)點操作移出節(jié)點removeChild()的使用,需要的朋友可以參考下2014-04-04
javascript兩種function的定義介紹及區(qū)別說明
javascript兩種function的定義方式function a(){}和a=function(){}具體使用如下,感興趣的朋友可以參考下,希望對你對你學習function的定義有所幫助2013-05-05
詳談構(gòu)造函數(shù)加括號與不加括號的區(qū)別
下面小編就為大家?guī)硪黄斦剺?gòu)造函數(shù)加括號與不加括號的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
為什么JS中eval處理JSON數(shù)據(jù)要加括號
這篇文章主要介紹了為什么JS中eval處理JSON數(shù)據(jù)要加括號的相關(guān)資料,需要的朋友可以參考下2015-04-04

