Mil*_*lar 26 jquery asp.net-mvc-3
我试图使用Jquery从下拉列表中获取所选文本.
<div>
@Html.DropDownList("SelectedCountryId", Model.CountryList, "(Select one Country)")
</div>
Run Code Online (Sandbox Code Playgroud)
下面给出的是我正在使用的Jquery.但这不起作用.我试过了
var selectedText1 = $("#SelectedCountryId").val($(this).find(":selected").text());
Run Code Online (Sandbox Code Playgroud)
并返回[对象对象].但是如何阅读所选文字?
接下来我试过了
var selectedText2 = $("#SelectedCountryId:selected").text();
Run Code Online (Sandbox Code Playgroud)
然后它返回空.
我也试过了
var selectedText2 = $("#SelectedCountryId option:selected").text();
Run Code Online (Sandbox Code Playgroud)
这也是空的.
我可以使用返回selectedID
var selectedID = $("#SelectedCountryId").val();
Run Code Online (Sandbox Code Playgroud)
但为什么不选择文字呢?
我的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 );
});
Run Code Online (Sandbox Code Playgroud)
这是我下面的下拉列表的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>
Run Code Online (Sandbox Code Playgroud)
Phi*_*ipp 62
昨天我遇到了同样的问题:-)
$("#SelectedCountryId option:selected").text()
Run Code Online (Sandbox Code Playgroud)
我也读到这很慢,如果你想经常使用它,你可能应该使用别的东西.
我不知道为什么你的不工作,这个是给我的,也许别人可以帮忙...
没有下拉ID:
$("#SelectedCountryId").change(function () {
$('option:selected', $(this)).text();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
120498 次 |
最近记录: |