如何在jQuery中从下拉列表中获得选定的文本(而不是选定的值)?
如果您已经在一个变量中使用了下拉列表,这就是我的工作方法。
$("option:selected", myVar).text()
关于这个问题的其他答案帮助了我,但最终jQuery论坛的帖子[$(this + "option:selected").attr("rel") option selected is not working in IE][1]帮助最大。
[1]: http://forum.jquery.com/topic/this-option-selected-attr-rel-option-selected-is-not-working-in-ie
更新。 修正了上述链接
这对我来说很有用
$("#dropdownid").change(function() {
alert($(this).find("option:selected").text());
});
如果动态创建的元素
$(document).on("change", "#dropdownid", function() {
alert($(this).find("option:selected").text());
});
这对我很有用。
$('#yourdropdownid').find('option:selected').text();
jQuery][1]版本。 1.9.1
请使用这个
var listbox = document.getElementById("yourdropdownid");
var selIndex = listbox.selectedIndex;
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
那么请提醒"selValue"。 和"selText"。 你会得到你选择的下拉值和文本
对于那些使用 [SharePoint][1] 列表且不想使用长时间生成的 id 的用户来说,这将会有效。
var e = $('select[title="IntenalFieldName"] option:selected').text();
$("#selectID option:selected").text();
你可以使用任何jQuery选择器代替#selectID
,比如.selectClass
使用class。
正如文档中提到的[这里][1]。
*:selected选择器适用于<option>.元素。
元素。
它对复选框或单选输入不起作用。
对它们使用:checked
。
.text() 根据文档[这里][2]。
*获取匹配元素集合中每个元素的组合文本内容,包括它们的子代。
所以你可以使用.text()
方法从任何HTML元素中获取文本。
更深层次的解释请参考文档。
[1]: http://api.jquery.com/selected-selector/ [2]: http://api.jquery.com/text/
在jQuery中的下拉/选择变化事件中选择文本和选定的值。
$("#yourdropdownid").change(function() {
console.log($("option:selected", this).text()); //text
console.log($(this).val()); //value
})