Answers:
我认为下拉菜单中的onchange事件的最佳和最短方法是获取所选选项:
$('option:selected',this);
获取value属性:
$('option:selected',this).attr('value');
在标签之间获取显示的部分:
$('option:selected',this).text();
在您的示例中:
$("#select-id").change(function(){
var cur_value = $('option:selected',this).text();
});
.context
,自jQuery 1.10起已弃用-api.jquery.com/category/deprecated/deprecated-1.10
var cur_value = $('option:selected',this).text();
var cur_value = $(this).find('option:selected').text();
由于option
很可能是select
您的直系子女,因此也可以使用:
var cur_value = $(this).children('option:selected').text();
option-selected
没有报价...?
find('option:selected')
为我返回一个空字符串。需要什么版本的jQuery?我在1.6.1中工作。children('option:selected')
确实有效。
option:selected
一直以来有1.0 api.jquery.com/selected-selector
只是
$(this).val();
我认为jQuery足够聪明,可以知道您的需求
$("option:selected", this)
如上所述进行了尝试,但这是有问题的。我使用按钮单击将选定的选项元素文本附加到另一个div,但是当我单击按钮时,它实际上更改了选定的元素……很奇怪。使用这个。