jQuery Jquery从下拉列表中获取SelectedText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10289721/
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
Jquery to get SelectedText from dropdown
提问by Millar
I am trying to get the selected Text from the dropdownlist using Jquery.
我正在尝试使用 Jquery 从下拉列表中获取选定的文本。
<div>
@Html.DropDownList("SelectedCountryId", Model.CountryList, "(Select one Country)")
</div>
Given below is the Jquery that I am using. But this is not working. I tried
下面给出的是我正在使用的 Jquery。但这是行不通的。我试过
var selectedText1 = $("#SelectedCountryId").val($(this).find(":selected").text());
and is returning [object object]. But how to read the selected text?
并且正在返回 [object object]。但是如何阅读选定的文本?
Next I tried
接下来我试过了
var selectedText2 = $("#SelectedCountryId:selected").text();
Then it's returning empty.
然后它返回空。
I also tried
我也试过
var selectedText2 = $("#SelectedCountryId option:selected").text();
This also returned empty.
这也返回空。
I am able to return the selectedID using
我可以使用返回 selectedID
var selectedID = $("#SelectedCountryId").val();
But why not the selected text?
但为什么不是选定的文本?
Is there anything wrong with my Jquery here? Please help
我的 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 );
});
This is the HTML for my dropdown below
这是我下面的下拉列表的 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>
回答by Philipp
I had the same problem yesterday :-)
我昨天遇到了同样的问题:-)
$("#SelectedCountryId option:selected").text()
I also read that this is slow, if you want to use it often you should probably use something else.
我还读到这很慢,如果您想经常使用它,您可能应该使用其他东西。
I don't know why yours is not working, this one is for me, maybe someone else can help...
我不知道为什么你的不起作用,这个是给我的,也许其他人可以帮助......
回答by MCurbelo
Without dropdown ID:
没有下拉ID:
$("#SelectedCountryId").change(function () {
$('option:selected', $(this)).text();
}
回答by Do Nhu Vy
回答by Rob Rodi
The problem could be on this line:
问题可能出在这一行:
var selectedText2 = $("#SelectedCountryId:selected").text();
It's looking for the item with id of SelectedCountryId
that is selected, where you really want the option that's selected underSelectedCountryId
, so try:
它寻找与标识的项SelectedCountryId
被选中,你真的想多数民众赞成选择的选项下SelectedCountryId
,这样试试:
$('#SelectedCountryId option:selected').text()
回答by Imran
first Set id attribute of dropdownlist like i do here than use that id to get value in jquery or javascrip.
首先像我在这里做的那样设置下拉列表的 id 属性,而不是使用该 id 在 jquery 或 javascript 中获取值。
dropdownlist:
下拉列表:
@Html.DropDownList("CompanyId", ViewBag.CompanyList as SelectList, "Select Company", new { @id="ddlCompany" })
jquery:
查询:
var id = jQuery("#ddlCompany option:selected").val();
回答by Gobind Gupta
//Code to Retrieve Text from the Dropdownlist
$('#selectOptions').change(function()
{
var selectOptions =$('#selectOptions option:selected');
if(selectOptions.length >0)
{
var resultString = "";
resultString = selectOptions.text();
}
});
回答by elclanrs
If you're using a <select>
, $(this).val()
inside the change()
event returns the value of the current selected option. Using text()
is redundant most of the time, since it's usually identical to the value, and in case is different, you'll probably end up using the value in the back-end and not the text. So you can just do this:
如果您使用的是<select>
,$(this).val()
则change()
事件内部将返回当前选定选项的值。text()
在大多数情况下使用是多余的,因为它通常与值相同,如果不同,您最终可能会在后端而不是文本中使用该值。所以你可以这样做:
http://jsfiddle.net/elclanrs/DW5kF/
http://jsfiddle.net/elclanrs/DW5kF/
var selectedText2 = $(this).val();
EDIT:Note that in case your value
attribute is empty, most browsers use the contents as value, so it'll work either way.
编辑:请注意,如果您的value
属性为空,大多数浏览器将内容用作值,因此无论哪种方式都可以使用。
回答by Thanh Trung
Get text from a dropdown multiple
从多个下拉列表中获取文本
var texts = [];
$('#list :selected').each(function(){
texts.push($(this).text());
});
texts now contains a list of selected text
文本现在包含选定文本的列表
回答by Bhanu Pratap
<%@ 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>
Thanks...:)
谢谢...:)
回答by Nirja Pathak
$("#SelectedCountryId_option_selected")[0].textContent
this worked for me, here instead of [0]
, pass the selected index of your drop down list.
这对我有用,在这里而不是[0]
传递下拉列表的选定索引。