如何使用JavaScript在表格单元格中获取所选的dropdownlist值?

时间:2014-03-18 12:58:19

标签: javascript html

我有一个像这样的HTML元素:

<td>
    <select>
        <option value="Polygon 47">Polygon 47</option>
        <option value="Polygon 49">Polygon 49</option>
    </select>
</td>

如何使用JavaScript访问当前选定的值?

3 个答案:

答案 0 :(得分:0)

我使用getElementsByTagName解决了这个问题。

        var e = cell.getElementsByTagName("select")[0];
        var myValue = e.options[e.selectedIndex].value);

答案 1 :(得分:-1)

解决方案:

<select id="shape">
<option value="Polygon 47">Polygon 47</option>
<option value="Polygon 49">Polygon 49</option>
</select>

<script type="text/javascript">
$(document).ready(function () {

    $('#shape').change(function(){
    var shapeId = document.getElementById("shape");
    var shapeChosen = shapeId.options[shapeId.selectedIndex].value;
    //Now use 'shapeChosen' wherever you want. 'shapeChosen' will give you the selected item.
   });
});
</script>

答案 2 :(得分:-1)

<select id="test">
<option value="Polygon 47">Polygon 47</option>
<option value="Polygon 49">Polygon 49</option>
</select>

$("#test").change(function() {alert($("#test option:selected").val())})