如何从 php 上的 javascript 的 html 下拉列表中获取选定的值和键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4509330/
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-25 13:56:48 来源:igfitidea点击:
How to get the selected value and key from an html dropdown list in javascript on php
提问by Ahmad Farid
<select id="facetList" style="width:120px;" class="text ui-widget-content ui-corner-all">
<?php
foreach($claAddArray as $k => $v)
{
echo "<option value=\"$k\">$v</option>";
}
?>
</select>
$("#btnSubmit").click(function() {
var $fId = ?????????????????
});
采纳答案by DampeS8N
jQuery's .val() will return the currently selected option's value if run on the select list itself. In your case $("#facetList").val();
如果在选择列表本身上运行,jQuery 的 .val() 将返回当前选定选项的值。在你的情况下$("#facetList").val();
use $("#facetList option:selected").text();for the text of the option tag.
使用$("#facetList option:selected").text();该选项标签的文本。
回答by seriousdev
document.getElementById('facetList').options[document.getElementById('facetList').selectedIndex].value

