Jquery从下拉列表中获取SelectedText
我想从下拉列表中使用Jquery获取选定的文本。Jquery从下拉列表中获取SelectedText
<div>
@Html.DropDownList("SelectedCountryId", Model.CountryList, "(Select one Country)")
</div>
下面给出的是我使用jQuery。但是这不起作用。 我试过
var selectedText1 = $("#SelectedCountryId").val($(this).find(":selected").text());
并返回[object object]。但如何阅读选定的文字?
下一个我试图
var selectedText2 = $("#SelectedCountryId:selected").text();
然后它返回空。
我也试过
var selectedText2 = $("#SelectedCountryId option:selected").text();
这也返回空。
我能够回到使用
var selectedID = $("#SelectedCountryId").val();
的selectedID但是,为什么不选择的文本?
我的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);
});
这低于
<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>
我的下拉列表中HTML昨天我有同样的问题:-)
$("#SelectedCountryId option:selected").text()
我也看了,这是缓慢的,如果你想要经常使用它,你应该使用别的东西。
我不知道为什么你不工作,这个人是我,也许别人可以帮助...
这个问题可能是在这条线:
var selectedText2 = $("#SelectedCountryId:selected").text();
它看起来对于具有SelectedCountryId
的ID选择的项目,在那里你真的想要一个选中后SelectedCountryId
下的选项,所以尝试:
$('#SelectedCountryId option:selected').text()
我试过$('#SelectedCountryId option:Selected')。文本() 但它返回空 – Millar 2012-04-23 23:41:49
我想我不小心大写'选择' – 2012-04-24 01:02:19
如果您使用的是<select>
,事件中的$(this).val()
将返回当前选定选项的值。在大多数情况下使用text()
是多余的,因为它通常与该值相同,如果情况不同,最终可能会在后端使用该值,而不是文本。所以,你可以只是这样做:
http://jsfiddle.net/elclanrs/DW5kF/
var selectedText2 = $(this).val();
编辑:请注意,如果您的value
属性是空的,大多数浏览器使用的内容价值,所以它会工作无论哪种方式。从下拉多
获取文本现在
var texts = [];
$('#list :selected').each(function(){
texts.push($(this).text());
});
文本包含选定的文本
没有下拉ID列表:
$("#SelectedCountryId").change(function() {
$('option:selected', $(this)).text();
}
$("#SelectedCountryId_option_selected")[0].textContent
这个工作对我来说,在这里,而不是[0]
,传递您的下拉列表中选定的索引。
请使用
var listbox = document.getElementById("yourdropdownid");
var selIndex = listbox.selectedIndex;
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
然后请提醒 “selValue” 和 “selText”。你得到你所选择下拉菜单的值和文本
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-3.1.0.js"></script>
<script>
$(function() {
$('#selectnumber').change(function(){
alert('.val() = ' + $('#selectnumber').val() + ' AND html() = ' + $('#selectnumber option:selected').html() + ' AND .text() = ' + $('#selectnumber option:selected').text());
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="selectnumber">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
</div>
</form>
</body>
</html>
谢谢... :)
第一套id属性就像我在这里做的比中使用该ID的jQuery的javascrip或中获取价值。
下拉列表:
@Html.DropDownList("CompanyId", ViewBag.CompanyList as SelectList, "Select Company", new { @id="ddlCompany" })
的jQuery:
var id = jQuery("#ddlCompany option:selected").val();
//Code to Retrieve Text from the Dropdownlist
$('#selectOptions').change(function()
{
var selectOptions =$('#selectOptions option:selected');
if(selectOptions.length >0)
{
var resultString = "";
resultString = selectOptions.text();
}
});
可能杜佩:http://*.com/questions/196684 – 2012-04-23 23:34:33
'你选择:” + selectedText1 + selectedText2'?据我所知,你可以用'select'标签选择一个选项! – undefined 2012-04-23 23:39:49