使用< select>获取select2中的自定义数据属性 [英] Get custom data-attribute in select2 with <select>

查看:454
本文介绍了使用< select>获取select2中的自定义数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下HTML5

 < select id =example> 
< option value =AAdata-id =143> AA< / option>
< option value =BBdata-id =344> BB< / option>
< / select>

$(#example)。select2();

如何从所选选项中获取数据ID?


使用select2没有直接方法,您可以使用select2数据和jQuery的组合方式:

  $( #示例)SELECT2()找到( :选择了)。数据( ID); 

首先获得select2数据,然后使用jQuery找到所选的选项,最后找到数据属性。

Let's assume you have the following HTML5

<select id="example">
    <option value="AA" data-id="143">AA</option>
    <option value="BB" data-id="344">BB</option>
</select>

$("#example").select2();

How do I get the data-id from the selected option ?

解决方案

There is no direct method with select2, you can use a combinaison of select2 data and jQuery :

$("#example").select2().find(":selected").data("id");

First you get the select2 data then you find your selected option with jQuery and finally the data-attribute.

这篇关于使用&lt; select&gt;获取select2中的自定义数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆