2012-04-23 166 views
23

我想从下拉列表中使用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> 
+0

可能杜佩:http://stackoverflow.com/questions/196684 – 2012-04-23 23:34:33

+0

'你选择:” + selectedText1 + selectedText2'?据我所知,你可以用'select'标签选择一个选项! – undefined 2012-04-23 23:39:49

回答

53

我的下拉列表中HTML昨天我有同样的问题:-)

$("#SelectedCountryId option:selected").text() 

我也看了,这是缓慢的,如果你想要经常使用它,你应该使用别的东西。

我不知道为什么你不工作,这个人是我,也许别人可以帮助...

+0

你是怎么解决的有问题吗? – Millar 2012-04-23 23:35:58

+0

$(“#SelectedCountryId option:selected”)。text() 对我不起作用,只是返回空的 – Millar 2012-04-23 23:43:45

+0

@Millar:哪个问题? – Philipp 2012-04-23 23:44:26

3

这个问题可能是在这条线:

  var selectedText2 = $("#SelectedCountryId:selected").text(); 

它看起来对于具有SelectedCountryId的ID选择的项目,在那里你真的想要一个选中后SelectedCountryId下的选项,所以尝试:

$('#SelectedCountryId option:selected').text() 
+0

我试过$('#SelectedCountryId option:Selected')。文本() 但它返回空 – Millar 2012-04-23 23:41:49

+0

我想我不小心大写'选择' – 2012-04-24 01:02:19

0

如果您使用的是<select>,事件中的$(this).val()将返回当前选定选项的值。在大多数情况下使用text()是多余的,因为它通常与该值相同,如果情况不同,最终可能会在后端使用该值,而不是文本。所以,你可以只是这样做:

http://jsfiddle.net/elclanrs/DW5kF/

var selectedText2 = $(this).val(); 

编辑:请注意,如果您的value属性是空的,大多数浏览器使用的内容价值,所以它会工作无论哪种方式。从下拉多

0

获取文本现在

var texts = []; 
$('#list :selected').each(function(){ 
    texts.push($(this).text()); 
}); 

文本包含选定的文本

8

没有下拉ID列表:

$("#SelectedCountryId").change(function() { 
    $('option:selected', $(this)).text(); 
} 
0
$("#SelectedCountryId_option_selected")[0].textContent 

这个工作对我来说,在这里,而不是[0],传递您的下拉列表中选定的索引。

0

请使用

var listbox = document.getElementById("yourdropdownid"); 
    var selIndex = listbox.selectedIndex; 
    var selValue = listbox.options[selIndex].value; 
    var selText = listbox.options[selIndex].text; 

然后请提醒 “selValue” 和 “selText”。你得到你所选择下拉菜单的值和文本

2

今天,使用jQuery,我这样做:

$("#foo").change(function(){  
    var foo = $("#foo option:selected").text(); 
}); 

\#foo是下拉框ID。

阅读more。下拉列表的

+0

应该保持这个代码在$(document).ready(function(){}); – kavie 2017-07-05 14:10:28

0
<%@ 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> 

Click to see Output Screen

谢谢... :)

1

第一套id属性就像我在这里做的比中使用该ID的jQuery的javascrip或中获取价值。

下拉列表:

@Html.DropDownList("CompanyId", ViewBag.CompanyList as SelectList, "Select Company", new { @id="ddlCompany" }) 

的jQuery:

var id = jQuery("#ddlCompany option:selected").val(); 
1
//Code to Retrieve Text from the Dropdownlist 

$('#selectOptions').change(function() 
{ 
    var selectOptions =$('#selectOptions option:selected'); 
    if(selectOptions.length >0) 
    { 
     var resultString = ""; 
     resultString = selectOptions.text(); 
    } 
}); 
相关问题