我正在尝试从当前选定的选项中获取数据。我出来了这个。 我可以以某种方式改进它吗?或者这样可以吗?
我特别不确定当前的选项选择器。
<option data-id='one'></option>
....
$('select#first').change(function(){
var smth = $("option:selected",this).data('id');
alert(smth);
});
最佳答案
看起来这应该可行,,
$('select#first').change(function() {
var smth = $(this).find('option:selected').attr('data-id');
alert(smth);
});
OR
$('select#first').change(function() {
var smth = $(this).find('option:selected').data('id');
alert(smth);
});
检查这个FIDDLE
关于javascript - 获取所选选项数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12378183/