简体   繁体   English

用户选择一个选项后,如何从选择标签中获取数据属性

[英]How can I get data-att from select tag after user select an option

HTML: HTML:

<div class="form-group ">
   <select class="form-control input-sm col-xs-2 game-select" data-game="LuckyTuesday">
        <option value="" disabled selected>Select Game </option>

        <option id="1" data-name="Direct 1" data-minNumber="1" data-maxNumber="1">
            Direct 1
        </option>
        <option id="2" data-name="Direct 2" data-minNumber="2" data-maxNumber="2">
            Direct 2
        </option>
   </select>
</div>

JavaScript: JavaScript:

$('select').change(function () {
    let gameType = $(this).find(':selected').attr('data-name');

    //How to can I use "game-select" class to get "data-game" att? 
});

你可以简单地使用data() jQuery 函数来获取data-game的值:

$(this).data('game');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的选择选项中获取要输入的数据 - How can I get data to input from my select option (Vue.js) 如何使用数据中的数值数组中的选项标签填充选择标签? - (Vue.js) How can I populate the select tag with option tag from array of number values in data? 我怎么能得到 <select>用户使用Flask选择的标签选项? - How can I get the <select> tag option chosen by the user using Flask? 如何将 select 标签中的选项传递给另一个 select 标签? - How can I pass an option in a select tag to another select tag? 如何从选择选项中获取HTML5数据值? - How can I get an HTML5 data value from a select option? 如何从我使用 select 选项的数据库中获取数据,以便它可以显示在输入框中 - How to get a data from database where i using select option so it can be shown in inputbox 获取“数据 - 某事” <option>标签包含在<select> - Get “data-something” from an <option> tag contained in <select> 如何在选择选项中返回数据 - How can I return the data in select option 如何使用jQuery数据表的onChange事件从html select标签选项值中获取值? - How to get value from html select tag option value using onChange event using jQuery data table? 如何在动态创建的选择标记中添加一个空选项? - How can I add an empty option in a select tag created dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM