[英]Get selected text from a drop-down list (select box) using jQuery
如何从 jQuery的下拉列表中获取选定的文本(不是选定的值)?
$("#yourdropdownid option:selected").text();
尝试这个:
$("#myselect :selected").text();
对于 ASP.NET 下拉列表,您可以使用以下选择器:
$("[id*='MyDropDownId'] :selected")
例如,此处发布的答案,
$('#yourdropdownid option:selected').text();
对我不起作用,但这确实:
$('#yourdropdownid').find('option:selected').text();
它可能是旧版本的 jQuery。
如果您已经在变量中提供了下拉列表,那么这对我有用:
$("option:selected", myVar).text()
关于这个问题的其他答案对我有帮助,但最终 jQuery 论坛线程$(this + "option:selected").attr("rel") option selected 在 IE 中不起作用。
更新:修复了上面的链接
这对我有用
$("#dropdownid").change(function() {
alert($(this).find("option:selected").text());
});
如果元素是动态创建的
$(document).on("change", "#dropdownid", function() {
alert($(this).find("option:selected").text());
});
$("option:selected", $("#TipoRecorde")).text()
$("#DropDownID").val()
将给出选定的索引值。
对于所选项目的文本,请使用:
$('select[name="thegivenname"] option:selected').text();
对于所选项目的值,请使用:
$('select[name="thegivenname"] option:selected').val();
各种方式
1. $("#myselect option:selected").text();
2. $("#myselect :selected").text();
3. $("#myselect").children(":selected").text();
4. $("#myselect").find(":selected").text();
用这个
const select = document.getElementById("yourSelectId");
const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;
然后你在selectedValue
和selectedText
获得你选择的值和文本。
$("#dropdownID").change(function(){
alert($('option:selected', $(this)).text());
});
var someName = "Test";
$("#<%= ddltest.ClientID %>").each(function () {
$('option', this).each(function () {
if ($(this).text().toLowerCase() == someName) {
$(this).attr('selected', 'selected')
};
});
});
这将帮助您找到正确的方向。 上面的代码已经过全面测试,如果您需要进一步的帮助,请告诉我。
对于那些使用SharePoint列表并且不想使用长生成 id 的人,这将起作用:
var e = $('select[title="IntenalFieldName"] option:selected').text();
$("select[id=yourDropdownid] option:selected").text()
这工作正常
为了获得选定的价值使用
$('#dropDownId').val();
并获取选定的项目文本使用此行:
$("#dropDownId option:selected").text();
$('#id').find('option:selected').text();
在 jQuery 中的下拉列表/选择更改事件中选择文本和选定值
$("#yourdropdownid").change(function() {
console.log($("option:selected", this).text()); //text
console.log($(this).val()); //value
})
尝试:
$var = jQuery("#dropdownid option:selected").val();
alert ($var);
或者要获取选项的文本,请使用text()
:
$var = jQuery("#dropdownid option:selected").text();
alert ($var);
更多信息:
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
只需尝试以下代码。
var text= $('#yourslectbox').find(":selected").text();
它返回所选选项的文本。
用:
('#yourdropdownid').find(':selected').text();
以下对我有用:
$.trim($('#dropdownId option:selected').html())
在兄弟情况下
<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()
这对我有用:
$("#city :selected").text();
我正在使用 jQuery 1.10.2
$(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>
这段代码对我有用。
$("#yourdropdownid").children("option").filter(":selected").text();
如果要将结果作为列表,请使用:
x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})
$("#dropdown").find(":selected").text();
$("#dropdown :selected").text();
$("#dropdown option:selected").text();
$("#dropdown").children(":selected").text();
尝试
dropdown.selectedOptions[0].text
function read() { console.log( dropdown.selectedOptions[0].text ); }
<select id="dropdown"> <option value="1">First</option> <option value="2">Second</option> </select> <button onclick="read()">read</button>
对于多选:
$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
$("#dropdownid option:selected").text();
如果您使用asp.net并编写
<Asp:dropdownlist id="ddl" runat="Server" />
那你应该用
$('#<%=ddl.Clientid%> option:selected').text();
只需添加以下行
$(this).prop('selected', true);
将 .att 替换为 .prop 它适用于所有浏览器。
$("#dropdownid").change(function(el) {
console.log(el.value);
});
或者你可以使用这个
$("#dropdownid").change(function() {
console.log($(this).val());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.