简体   繁体   中英

jQuery select2 - Data attribute with ajax

通过select2,我可以使用select2 ajax方法在所有<option>标记上设置属性“ data”以采用<select>的值吗?

Try the following code

$(document).ready(function(){
    var options = '<option value="test1" data-temp="test1_data">test1</option><option value="test2" data-temp="test2_data">test2</option>';

   $("#test").empty().append(options).select2();

        $("#test").on('change',function(){

        alert($(this).find(":selected").data('temp'));
   });
});

JSBIN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM