作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
用户选择多个值。我如何获取每个选定的数据属性值。
HTML 代码为
<select id="EMaddMoreEmployee" name="EMaddMoreEmployee[]" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true">
<option value="3" data-available_text="Prashant Kumar is unable to work today.">Prashant Kumar</option>
<option value="4" selected="" data-available_text="Anand Kumar has not given availability for this day.">Anand Kumar</option>
<option value="7" data-available_text="Manoj Kumar has not given availability for this day.">Manoj Kumar</option>
<option value="8" data-available_text="Delip Kumar is available to work all day.">Delip Kumar</option>
<option value="9" data-available_text="Purendar Kumar has not given availability for this day.">Purendar Kumar</option>
<option value="10" data-available_text="Subhas Kumar has not given availability for this day.">Subhas Kumar</option>
<option value="11" data-available_text="Hera Dheware has not given availability for this day.">Hera Dheware</option>
<option value="12" data-available_text="Puspa Di has not given availability for this day.">Puspa Di</option>
</select>
Jquery 代码是
$('#EMaddMoreEmployee').on('change', function()
{
var selected = $(this).find('option:selected', this);
var availble_text = selected.data('available_text');
console.log(availble_text);
});
最佳答案
您将获得一系列选定的选项。
$('#EMaddMoreEmployee').on('change', function() {
var selected = $(this).find('option:selected', this);
var results = [];
selected.each(function() {
results.push($(this).data('available_text'));
});
console.log(results);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="EMaddMoreEmployee" name="EMaddMoreEmployee[]" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true">
<option value="3" data-available_text="Prashant Kumar is unable to work today.">Prashant Kumar</option>
<option value="4" selected="" data-available_text="Anand Kumar has not given availability for this day.">Anand Kumar</option>
<option value="7" data-available_text="Manoj Kumar has not given availability for this day.">Manoj Kumar</option>
<option value="8" data-available_text="Delip Kumar is available to work all day.">Delip Kumar</option>
<option value="9" data-available_text="Purendar Kumar has not given availability for this day.">Purendar Kumar</option>
<option value="10" data-available_text="Subhas Kumar has not given availability for this day.">Subhas Kumar</option>
<option value="11" data-available_text="Hera Dheware has not given availability for this day.">Hera Dheware</option>
<option value="12" data-available_text="Puspa Di has not given availability for this day.">Puspa Di</option>
</select>
关于jquery - 如何在多选选项中获取所选选项数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47235478/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!