本文實例講述了javascript獲取select值的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>www.49028c.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("請輸入內容") }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>
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答