close
廢話不多說,
馬上來看原始碼:
設定:
<HTML> <form name="testForm" > <!-- 其他屬性(attribute)請自行添加. --> <input value="2" type="hidden" name="pageIndex" /> <select name="pageIndexA" > <option value="1" selected >1</option> <option value="2">2</option> <option value="3">3</option> </select> </form> <script> SetPage( document.forms['testForm'] ); </script> </HTML>
function SetPage( form ) { var userSelectedVal = form.elements['pageIndex'].value ; var selector = form.elements['pageIndexA'] ; var opts = selector.options; for ( var opt, j=0; j<opts.length ; j++ ) { opt = opts[j]; if ( opt.value == userSelectedVal ) { selector.selectedIndex = j; break; } } // for } // SetPage()
參考資料:Changing the selected option of an HTML Select element
取得:
<select id="ddlViewBy"> <option value="1">test1</option> <option value="2" selected="selected">test2</option> <option value="3">test3</option> </select>
var e = document.getElementById("ddlViewBy"); var strUser = e.options[e.selectedIndex].value;
var e = document.getElementById("ddlViewBy"); var strUser = e.options[e.selectedIndex].text;
參考資料:Get selected value in dropdown list using JavaScript?
文章標籤
全站熱搜
留言列表