我正在尝试使用Jquery从下拉列表中获取选定的文本。
<div>
@Html.DropDownList("SelectedCountryId", Model.CountryList, "(Select one Country)")
</div>下面给出的是我正在使用的Jquery。但这是行不通的。我试过了
var selectedText1 = $("#SelectedCountryId").val($(this).find(":selected").text()); 并返回object对象。但是如何阅读所选的文本呢?
接下来我试着
var selectedText2 = $("#SelectedCountryId:selected").text();然后它返回为空。
我也试过了
var selectedText2 = $("#SelectedCountryId option:selected").text();这也返回了空。
我可以使用以下命令返回selectedID
var selectedID = $("#SelectedCountryId").val();但为什么不是选定的文本呢?
我的Jquery有什么问题吗?请帮帮忙
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#SelectedCountryId").change(function () {
var selectedText1 = $("#SelectedCountryId").val($(this).find(":selected").text());
var selectedText2 = $("#SelectedCountryId:selected").text();
alert("You selected :" + selectedText1 + selectedText2 );
});下面是我的下拉列表的HTML
<select id="SelectedCountryId" name="SelectedCountryId"><option value="">(Select one Country)</option>
<option value="19">USA</option>
<option value="10">Germany</option>
<option value="12">Australia</option> </select>发布于 2012-04-24 07:34:04
我昨天遇到了同样的问题:-)
$("#SelectedCountryId option:selected").text()我还读到这很慢,如果你想经常使用它,你可能应该使用其他东西。
我不知道为什么你的不能工作,这个是给我的,也许其他人可以帮助你…
发布于 2013-08-27 23:55:24
没有dropdown ID:
$("#SelectedCountryId").change(function () {
$('option:selected', $(this)).text();
}发布于 2015-06-25 17:16:30
今天,使用jQuery,我这样做:
$("#foo").change(function(){
var foo = $("#foo option:selected").text();
});\#foo是下拉框id。
阅读more。
https://stackoverflow.com/questions/10289721
复制相似问题