【发布时间】:2015-12-20 03:12:35
【问题描述】:
我正在尝试使用 Select2 ver4 jquery 插件和使用 Select2 示例页面的加载远程数据进行 AJAX 调用。现在我尝试按 Enter 键下一个元素焦点。
HTML 代码
<table>
<tr>
<td>
<select class="rp">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="rp">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="js-example-data-array"></select>
</td>
</tr>
</table>
JS 代码
$('select:first').focus();
$('input, select, textarea').on('keydown', function(e) {
if (e.which == 13) {
if (e.ctrlKey) {
$(this).closest('form').submit();
} else {
var fields = $(this).closest('form').find('input, select, textarea');
var total = fields.length;
var index = fields.index(this);
fields.eq( index + (e.shiftKey ? (index > 0 ? -1 : 0 ) : (index < total ? +1 : total ) ) ).focus();
return false;
}
}
});
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
$(".js-example-data-array").select2({
data: data
})
https://jsfiddle.net/ojpcsyxd/
我想通过按回车键来聚焦下一个元素。但我无法聚焦 select2 元素。
【问题讨论】:
标签: jquery jquery-select2 jquery-select2-4