选择更改时,获取数据属性值 [英] On select change, get data attribute value
本文介绍了选择更改时,获取数据属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码返回未定义" ...
The following code returns 'undefined'...
$('select').change(function(){
alert($(this).data('id'));
});
<select>
<option data-id="1">one</option>
<option data-id="2">two</option>
<option data-id="3">three</option>
</select>
推荐答案
您需要找到选定的选项:
You need to find the selected option:
$(this).find(':selected').data('id')
或
$(this).find(':selected').attr('data-id')
尽管首选第一种方法.
这篇关于选择更改时,获取数据属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文