Get selected text from a drop-down list (select box) using jQuery
如何从jquery的下拉列表中获取所选文本(不是所选值)?
| 1 | $("#yourdropdownid option:selected").text(); | 
试试这个:
| 1 | $("#myselect :selected").text(); | 
对于ASP.NET下拉列表,可以使用以下选择器:
| 1 | $("[id*='MyDropDownId'] :selected") | 
例如,这里发布的答案,
| 1 | $('#yourdropdownid option:selected').text(); | 
不适合我,但这确实:
| 1 | $('#yourdropdownid').find('option:selected').text(); | 
它可能是jquery的旧版本。
如果变量中已经有DropDownList可用,那么这对我很有用:
| 1 | $("option:selected", myVar).text() | 
关于这个问题的其他答案帮助了我,但最终jquery论坛线程$(this+"option:selected").attr("rel")选项在IE中的作用最大。
更新:修复了上述链接
| 1 | $("option:selected", $("#TipoRecorde")).text() | 
这对我有用
| 1 2 3 | $("#dropdownid").change(function() { alert($(this).find("option:selected").text()); }); | 
如果元素是动态创建的
| 1 2 3 | $(document).on("change","#dropdownid", function() { alert($(this).find("option:selected").text()); }); | 
这对我很有用:
| 1 | $('#yourdropdownid').find('option:selected').text(); | 
jquery版本:1.9.1
对于所选项目的文本,请使用:
| 1 | $('select[name="thegivenname"] option:selected').text(); | 
对于所选项目的值,请使用:
| 1 | $('select[name="thegivenname"] option:selected').val(); | 
各种方式
| 1 2 3 4 5 6 7 | 1. $("#myselect option:selected").text(); 2. $("#myselect :selected").text(); 3. $("#myselect").children(":selected").text(); 4. $("#myselect").find(":selected").text(); | 
| 1 2 3 | $("#dropdownID").change(function(){ alert($('option:selected', $(this)).text()); }); | 
| 1 2 3 4 5 6 7 8 9 | var someName ="Test"; $("#<%= ddltest.ClientID %>").each(function () { $('option', this).each(function () { if ($(this).text().toLowerCase() == someName) { $(this).attr('selected', 'selected') }; }); }); | 
这将帮助你找到正确的方向。如果您需要进一步的帮助,请告诉我以上代码经过了全面测试。
请使用此
| 1 2 3 4 | var listbox = document.getElementById("yourdropdownid"); var selIndex = listbox.selectedIndex; var selValue = listbox.options[selIndex].value; var selText = listbox.options[selIndex].text; | 
然后请提醒"selvalue"和"seltext"。您将得到所选的下拉值和文本
对于使用SharePoint列表且不想使用长生成ID的用户,这将有效:
| 1 | var e = $('select[title="IntenalFieldName"] option:selected').text(); | 
| 1 |  $("#selectID option:selected").text(); | 
您可以使用任何jquery选择器,而不是
如本文所述。
:selected选择器适用于
.text()根据此处的文档。
获取匹配元素集中每个元素的组合文本内容,包括它们的后代。
因此,您可以使用
请参阅文档以获得更深入的解释。
| 1 | $("select[id=yourDropdownid] option:selected").text() | 
这很好
| 1 | $('#id').find('option:selected').text(); | 
用于获取选定值
| 1 | $('#dropDownId').val(); | 
要获取所选项目文本,请使用以下行:
| 1 | $("#dropDownId option:selected").text(); | 
下拉选择文本和所选值/在jquery中选择更改事件
| 1 2 3 4 | $("#yourdropdownid").change(function() { console.log($("option:selected", this).text()); //text console.log($(this).val()); //value }) | 
用途:
| 1 | ('#yourdropdownid').find(':selected').text(); | 
以下内容对我很有用:
| 1 | $.trim($('#dropdownId option:selected').html()) | 
| 1 2 | var e = document.getElementById("dropDownId"); var div = e.options[e.selectedIndex].text; | 
兄弟姐妹案
| 1 2 3 4 5 6 7 8 9 10 | ADD Client <input type="text" placeholder="Enter client name" style="margin: 5px;float: right" class="clientsearch large" /> <select class="mychzn-select clientList"> <option value="">Select Client name....</option> <option value="1">abc</option> </select> /*jQuery*/ $(this).siblings('select').children(':selected').text() | 
这是我的工作:
| 1 | $("#city :selected").text(); | 
我正在使用jquery 1.10.2
尝试:
| 1 2 | $var = jQuery("#dropdownid option:selected").val(); alert ($var); | 
或使用
| 1 2 | $var = jQuery("#dropdownid option:selected").text(); alert ($var); | 
更多信息:
- 网址:http://api.jquery.com/val/
- http://api.jquery.com/text/文本/
| 1 2 3 | $(function () { alert('.val() = ' + $('#selectnumber').val() + ' AND html() = ' + $('#selectnumber option:selected').html() + ' AND .text() = ' + $('#selectnumber option:selected').text()); }); | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> </head> <body> <form id="form1" runat="server"> <select id="selectnumber"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> <option value="4">four</option> </select> </form> </body> </html> | 

只需尝试以下代码。
| 1 | var text= $('#yourslectbox').find(":selected").text(); | 
它返回所选选项的文本。
如果要将结果作为列表,请使用:
| 1 2 | x=[]; $("#list_id").children(':selected').each(function(){x.push($(this).text());}) | 
只需添加下一行
| 1 | $(this).prop('selected', true); | 
替换了.att to.prop它适用于所有浏览器。
对于多重选择:
| 1 | $("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); } | 
| 1 | $("#dropDownId option:selected").text(); | 
如果使用ASP.NET并写入
| 1 | <Asp:dropdownlist id="ddl" runat="Server" /> | 
那你应该用
| 1 | $('#<%=ddl.Clientid%> option:selected').text(); |