本文實例講述了javascript獲取select值的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>m.survivalescaperooms.com 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.VeVB.COm/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è)計有所幫助。
新聞熱點
疑難解答