如何从jQuery的下拉列表中获取所选文本(而不是选定的值)?
试试这个:
$("#myselect :selected").text();
对于ASP.NET下拉列表,您可以使用以下选择器:
$("[id*='MyDropDownId'] :selected")
$("#DropDownID").val() 将给出所选的索引值 .
$("#DropDownID").val()
只需尝试以下代码即可 .
var text= $('#yourslectbox').find(":selected").text();
它返回所选选项的文本 .
$("option:selected", $("#TipoRecorde")).text()
$("#dropdownID").change(function(){ alert($('option:selected', $(this)).text()); });
这对我有用:
$('#yourdropdownid').find('option:selected').text();
jQuery版本:1.9.1
var e = document.getElementById("dropDownId"); var div = e.options[e.selectedIndex].text;
$("select[id=yourDropdownid] option:selected").text()
这很好用
对于多选:
$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
例如,这里发布的答案,
$('#yourdropdownid option:selected').text();
不适合我,但这样做:
它可能是jQuery的旧版本 .
$("#selectID option:selected").text();
您可以使用任何jQuery选择器代替 #selectID ,例如 .selectClass 使用类 .
#selectID
.selectClass
如文档here中所述 .
:selected选择器适用于<option>元素 . 它不适用于复选框或无线电输入;使用 :checked 为他们 .
:checked
.text() 根据文档here .
获取匹配元素集中每个元素的组合文本内容,包括它们的后代 .
因此,您可以使用 .text() 方法从任何HTML元素中获取文本 .
.text()
请参阅文档以获得更深入的解释 .
在兄弟情况下
<a class="uibutton confirm addClient" href="javascript:void(0);">ADD Client</a> <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()
var someName = "Test"; $("#<%= ddltest.ClientID %>").each(function () { $('option', this).each(function () { if ($(this).text().toLowerCase() == someName) { $(this).attr('selected', 'selected') }; }); });
这将有助于您获得正确的方向 . 如果您需要进一步的帮助,请通过以上代码进行全面测试 .
对于所选项目的文本,请使用:
$('select[name="thegivenname"] option:selected').text();
对于所选项目的值,请使用:
$('select[name="thegivenname"] option:selected').val();
在下拉列表中选择文本和选定的值/在jQuery中选择更改事件
$("#yourdropdownid").change(function() { console.log($("option:selected", this).text()); //text console.log($(this).val()); //value })
$(function () { alert('.val() = ' + $('#selectnumber').val() + ' AND html() = ' + $('#selectnumber option:selected').html() + ' AND .text() = ' + $('#selectnumber option:selected').text()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </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>
如果您希望将结果作为列表,请使用:
x=[]; $("#list_id").children(':selected').each(function(){x.push($(this).text());})
$("#yourdropdownid option:selected").text();
尝试:
$var = jQuery("#dropdownid option:selected").val(); alert ($var);
或者要获取选项的文本,请使用 text() :
text()
$var = jQuery("#dropdownid option:selected").text(); alert ($var);
More Info:
http://api.jquery.com/val/
http://api.jquery.com/text/
$("#dropdownid option:selected").text();
如果你使用asp.net并写
<Asp:dropdownlist id="ddl" runat="Server" />
那你应该用
$('#<%=ddl.Clientid%> option:selected').text();
使用:
('#yourdropdownid').find(':selected').text();
请用这个
var listbox = document.getElementById("yourdropdownid"); var selIndex = listbox.selectedIndex; var selValue = listbox.options[selIndex].value; var selText = listbox.options[selIndex].text;
然后请提醒“selValue”和“selText” . 您将获得所选的下拉值和文本
对于那些使用SharePoint列表并且不想使用long生成的id的人,这将起作用:
var e = $('select[title="IntenalFieldName"] option:selected').text();
这项工作对我来说:
$("#city :selected").text();
我正在使用jQuery 1.10.2
这对我有用
$("#dropdownid").change(function() { alert($(this).find("option:selected").text()); });
如果元素是动态创建的
$(document).on("change", "#dropdownid", function() { alert($(this).find("option:selected").text()); });
以下对我有用:
$.trim($('#dropdownId option:selected').html())
如果您已经在变量中提供了下拉列表,这对我有用:
$("option:selected", myVar).text()
关于这个问题的其他答案对我有帮助,但最终jQuery论坛帖子$(this + "option:selected").attr("rel") option selected is not working in IE帮助最多 .
更新:修复了上述链接
$('#id').find('option:selected').text();
用于获取选定的值
$('#dropDownId').val();
并获取所选项目文本使用此行:
$("#dropDownId option:selected").text();
各种方式
1. $("#myselect option:selected").text(); 2. $("#myselect :selected").text(); 3. $("#myselect").children(":selected").text(); 4. $("#myselect").find(":selected").text();
30 回答
试试这个:
对于ASP.NET下拉列表,您可以使用以下选择器:
$("#DropDownID").val()
将给出所选的索引值 .只需尝试以下代码即可 .
它返回所选选项的文本 .
这对我有用:
jQuery版本:1.9.1
这很好用
对于多选:
例如,这里发布的答案,
不适合我,但这样做:
它可能是jQuery的旧版本 .
您可以使用任何jQuery选择器代替
#selectID
,例如.selectClass
使用类 .如文档here中所述 .
:selected选择器适用于<option>元素 . 它不适用于复选框或无线电输入;使用
:checked
为他们 ..text() 根据文档here .
获取匹配元素集中每个元素的组合文本内容,包括它们的后代 .
因此,您可以使用
.text()
方法从任何HTML元素中获取文本 .请参阅文档以获得更深入的解释 .
在兄弟情况下
这将有助于您获得正确的方向 . 如果您需要进一步的帮助,请通过以上代码进行全面测试 .
对于所选项目的文本,请使用:
对于所选项目的值,请使用:
在下拉列表中选择文本和选定的值/在jQuery中选择更改事件
如果您希望将结果作为列表,请使用:
尝试:
或者要获取选项的文本,请使用
text()
:More Info:
http://api.jquery.com/val/
http://api.jquery.com/text/
如果你使用asp.net并写
那你应该用
使用:
请用这个
然后请提醒“selValue”和“selText” . 您将获得所选的下拉值和文本
对于那些使用SharePoint列表并且不想使用long生成的id的人,这将起作用:
这项工作对我来说:
我正在使用jQuery 1.10.2
这对我有用
如果元素是动态创建的
以下对我有用:
如果您已经在变量中提供了下拉列表,这对我有用:
关于这个问题的其他答案对我有帮助,但最终jQuery论坛帖子$(this + "option:selected").attr("rel") option selected is not working in IE帮助最多 .
更新:修复了上述链接
用于获取选定的值
并获取所选项目文本使用此行:
各种方式