【发布时间】:2019-09-07 06:09:42
【问题描述】:
我创建了一个 jQuery 代码,它应该显示选定的选项以及选定的值。但是,如果我选择“状态”选项,则输出为:
“你有 Selected[object Object] 并且它的值是 state”
$("document").ready(function() {
$("#location").prepend("<option value=\"country\">country</option>");
$("#location").append("<option value=\"city\">city</option>");
$("#location").append("<option value=\"area\">Area</option>");
$("#gender").append("<option value=\"male\">Male</option>");
$("#gender").append("<option value=\"female\">Female</option>");
$("#gender").append("<option value=\"other\">Other</option>");
$('select').on('change', function(e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
$("h2").html("You had Selected " + optionSelected + "and its value is " + valueSelected);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<select id="location">
<!--Default option-->
<option selected="selected">Location</option>
<option>state</option>
</select>
<select id="gender">
<!--Default option-->
<option selected="selected">Gender</option>
</select>
<br>
<h2></h2>
如何解决 [object Object] 并显示我的选项?
【问题讨论】:
标签: jquery tags option selected