如何使用 JavaScript 在表格单元格中获取下拉列表的选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22480129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:14:37 来源:igfitidea点击:
How to get the selected value of dropdownlist inside a table cell using JavaScript?
提问by ustroetz
I have an HTML element like this:
我有一个像这样的 HTML 元素:
<td>
<select>
<option value="Polygon 47">Polygon 47</option>
<option value="Polygon 49">Polygon 49</option>
</select>
</td>
How can I access the currently selected value with JavaScript?
如何使用 JavaScript 访问当前选定的值?
回答by ustroetz
I solved it by using getElementsByTagName
.
我通过使用解决了它getElementsByTagName
。
var e = cell.getElementsByTagName("select")[0];
var myValue = e.options[e.selectedIndex].value);
回答by Aniruddha Pondhe
Solution:
解决方案:
<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>
回答by Branden Tanga
<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())})