这篇文章原先写在Segmentfault的账号上面,现在拷贝到这里~
链接地址:https://segmentfault.com/a/1190000015075258
HTML片段展示
话不多说,咱就直奔主题吧,直接展示代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <body> <select name="n_select" class="c_select" id="i_select1"> <option value="1">1</option> <option value="2">2</option> <option value="3" selected>3</option> <option value="4">4</option> <option value="5">5</option> </select> <select name="n_select" class="c_select" id="i_select2"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4" selected >4</option> <option value="5">5</option> </select> </body>
|
JavaScript片段展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <script type="text/javascript">
$('[name=n_select] :selected'); $('.c_select option:selected'); $('.c_select').find('option:selected'); $('[name=n_select] :selected').text(); $('[name=n_select] :selected').val(); $('.c_select option:selected').text(); $('.c_select option:selected').val(); $('.c_select').find('option:selected').text(); $('.c_select').find('option:selected').val();
var tag_select = document.getElementsByClassName('c_select'); var tag_select = document.getElementsByTagName('select'); var tag_select = document.getElementsByName('n_select'); var tag_sValue = tag_select[1].value; var tag_sIndex = tag_select[1].selectedIndex; var tag_oValue = tag_select[1].options[tag_sIndex].value; var tag_oTexts = tag_select[1].options[tag_sIndex].text; </script>
|
Tips:温馨提示
我在写代码 js 部分的时候 tag_select[1].options[tag_sIndex] 中的 options 老是忽略 s 写成 option,导致获取不到值。和我一样粗心大意的童鞋需要注意下,不要再犯这种低级错误啦 ♪(^∇^*)!