js获取select标签选中的值

来源:互联网 发布:知合控股有限公司 编辑:程序博客网 时间:2024/04/30 05:16
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js">
</script>
<table>   <tr>
                         <td><B>11111</B></td>
                         <td align="center"><B>2222</B></td>
                         <td align="center"><B>3333</B></td>
                         <td class="result"
                         <select id="grade">
                              <option value ="1">1</option>
                              <option value ="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                              <option value="5">5</option>
                        </select></td>
                         <td><B><input type="button" class="btn"  value="提交"/></B></td>
  </tr> <tr>
                         <td><B>11111</B></td>
                         <td align="center"><B>2222</B></td>
                         <td align="center"><B>3333</B></td>
                         <td class="result"
                         <select id="grade">
                              <option value ="1">1</option>
                              <option value ="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                              <option value="5">5</option>
                        </select></td>
                         <td><B><input type="button" class="btn"  value="提交"/></B></td>
  </tr> <tr>
                         <td><B>11111</B></td>
                         <td align="center"><B>2222</B></td>
                         <td align="center"><B>3333</B></td>
                         <td class="result"
                         <select id="grade">
                              <option value ="1">1</option>
                              <option value ="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                              <option value="5">5</option>
                        </select></td>
                         <td><B><input type="button" class="btn"  value="提交"/></B></td>
  </tr></table>
<input type="button" value="获取" onclick="alert('第2行select:'+$('table tr:eq(1) select').val())"/>
<script>
  $('.btn').click(function(){alert($(this).parents('tr').find('select').val())})

  </script>



js获取select标签选中的值


-----------------------------***********************************************************--------------------------------

var obj = document.getElementByIdx_x(”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;//索引

 

第二种方式
$("#tesetSelect").find("option:selected").text();//选中的文本
…….val();
…….get(0).selectedIndex;


0 0
原创粉丝点击