select2 在 select 之后改变焦点
select2 change focus after select
嘿,所以我有一些 select2 选项,在我 select 我的第一个下拉选项之后,它进入事件侦听器并打印“用户 selected”。然而,焦点并没有转移到下一个 select2。非常感谢帮助。
<select id = "test1" required tabindex="1">
<option value = '1'>item1 </option>
</select>
<select id = "test2" required tabindex="2">
<option value = '2'>item1 </option>
</select>
<select id="test3" required tabindex="3" style="width: 30%; height:36px;">
<option value='1'>item1 </option>
<option value='2'>item2 </option>
</select>
<select id="test4" required tabindex="4" style="width: 30%; height:36px;">
<option value='1'>item1 </option>
<option value='2'>item2 </option>
</select>
$('#test1').select2({ });
$('#test2').select2({ });
$('#test3').select2({});
$('#test4').select2({});
$('select').on('select2:select', function () {
$(this).next('select').focus();
// Do something
console.log('user selected');
});
您可以使用 $(this).nextAll("select:first")
获取兄弟姐妹 select 的参考,然后使用 .select2("open")
打开您的下一个 select-box 。
演示代码 :
$('#test1').select2({});
$('#test2').select2({});
$('#test3').select2({});
$('#test4').select2({});
$('select').on('select2:select', function() {
//use select or use select#yourid where you need to focus
$(this).nextAll('select:first').select2('open');
console.log('user selected');
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<select id="test1" required tabindex="1" style="width: 30%; height:36px;">
<option value='1'>item1 </option>
<option value='2'>item2 </option>
</select>
<select id="test2" required tabindex="2" style="width: 30%; height:36px;">
<option value='2'>item1 </option>
<option value='5'>item5</option>
</select>
<select id="test3" required tabindex="2" style="width: 30%; height:36px;">
<option value='2'>item1 </option>
<option value='5'>item5</option>
</select>
<select id="test4" required tabindex="2" style="width: 30%; height:36px;">
<option value='2'>item1 </option>
<option value='5'>item5</option>
</select>
嘿,所以我有一些 select2 选项,在我 select 我的第一个下拉选项之后,它进入事件侦听器并打印“用户 selected”。然而,焦点并没有转移到下一个 select2。非常感谢帮助。
<select id = "test1" required tabindex="1">
<option value = '1'>item1 </option>
</select>
<select id = "test2" required tabindex="2">
<option value = '2'>item1 </option>
</select>
<select id="test3" required tabindex="3" style="width: 30%; height:36px;">
<option value='1'>item1 </option>
<option value='2'>item2 </option>
</select>
<select id="test4" required tabindex="4" style="width: 30%; height:36px;">
<option value='1'>item1 </option>
<option value='2'>item2 </option>
</select>
$('#test1').select2({ });
$('#test2').select2({ });
$('#test3').select2({});
$('#test4').select2({});
$('select').on('select2:select', function () {
$(this).next('select').focus();
// Do something
console.log('user selected');
});
您可以使用 $(this).nextAll("select:first")
获取兄弟姐妹 select 的参考,然后使用 .select2("open")
打开您的下一个 select-box 。
演示代码 :
$('#test1').select2({});
$('#test2').select2({});
$('#test3').select2({});
$('#test4').select2({});
$('select').on('select2:select', function() {
//use select or use select#yourid where you need to focus
$(this).nextAll('select:first').select2('open');
console.log('user selected');
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<select id="test1" required tabindex="1" style="width: 30%; height:36px;">
<option value='1'>item1 </option>
<option value='2'>item2 </option>
</select>
<select id="test2" required tabindex="2" style="width: 30%; height:36px;">
<option value='2'>item1 </option>
<option value='5'>item5</option>
</select>
<select id="test3" required tabindex="2" style="width: 30%; height:36px;">
<option value='2'>item1 </option>
<option value='5'>item5</option>
</select>
<select id="test4" required tabindex="2" style="width: 30%; height:36px;">
<option value='2'>item1 </option>
<option value='5'>item5</option>
</select>