如何从jQuery的下拉列表中获取所选文本(而不是所选值)?
当前回答
$("#dropdownid").change(function(el) {
console.log(el.value);
});
或者你可以用这个
$("#dropdownid").change(function() {
console.log($(this).val());
});
其他回答
这对我来说很有用:
$("#city :selected").text();
我正在使用jQuery 1.10.2
在兄弟姐妹的情况下
<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()
$('#id').find('option:selected').text();
$("#selectID option:selected").text();
您可以使用任何jQuery选择器来代替#selectID,例如.selectClass using class。
如本文文档中所述。
:selected选择器适用于<option>元素。它不适用于复选框或无线电输入;用途:已检查。
.text()根据此处的文档。
获取匹配元素集合中每个元素的组合文本内容,包括其后代。
因此,您可以使用.text()方法从任何HTML元素中获取文本。
有关详细说明,请参阅文档。
$("#dropdownid").change(function(el) {
console.log(el.value);
});
或者你可以用这个
$("#dropdownid").change(function() {
console.log($(this).val());
});