Ответ 1
$('#fieldNext').click(function() {
$('#selectionChamp option:selected').next().attr('selected', 'selected');
alert($('#selectionChamp').val());
});
Я пытаюсь создать кнопку, которая может выбрать следующую опцию.
Итак, у меня есть select (id = selectionChamp) с несколькими параметрами, следующий вход (id = fieldNext), и я пытаюсь сделать это:
$('#fieldNext').click(function() {
$('#selectionChamp option:selected', 'select').removeAttr('selected')
.next('option').attr('selected', 'selected');
alert($('#selectionChamp option:selected').val());
});
Но я не могу выбрать следующий вариант. Спасибо!
$('#fieldNext').click(function() {
$('#selectionChamp option:selected').next().attr('selected', 'selected');
alert($('#selectionChamp').val());
});
$("#fieldNext").click(function() {
$("#selectionChamp > option:selected")
.prop("selected", false)
.next()
.prop("selected", true);
});
Довольно просто без jQuery. При достижении последнего из них он будет перемещаться к первому варианту:
function nextOpt() {
var sel = document.getElementById('selectionChamp');
var i = sel.selectedIndex;
sel.options[++i%sel.options.length].selected = true;
}
window.onload = function() {
document.getElementById('fieldNext').onclick = nextOpt;
}
Некоторая тестовая разметка:
<button id="fieldNext">Select next</button>
<select id="selectionChamp">
<option>0
<option>1
<option>2
</select>
$(function(){
$('#button').on('click', function(){
var selected_element = $('#selectionChamp option:selected');
selected_element.removeAttr('selected');
selected_element.next().attr('selected', 'selected');
$('#selectionChamp').val(selected_element.next().val());
});
});
$('#fieldNext').click(function() {
$('#selectionChamp option:selected').removeAttr('selected')
.next('option').attr('selected', 'selected');
alert($('#selectionChamp option:selected').val());
});
Попробуйте следующее:
$(document).ready(function(){
$("#fieldNext").on("click",function(){
$optionSelected = $("#selectionChamp > option:selected");
$optionSelected.removeAttr("selected");
$optionSelected.next("option").attr("selected","selected");
});
});
Другие решения не работают, если в дополнение к элементам опции есть элементы optiongroup. В этом случае это работает:
var options = $("#selectionChamp option");
var i = options.index(options.filter(":selected"));
if (i >= 0 && i < options.length - 1) {
options.eq(i+1).prop("selected", true);
}
(Вы можете подумать, что выражение для i
также может быть записано как options.index(":selected")
, но это не всегда работает. Я не знаю, почему, объяснение будет приветствоваться.)
Я бы ожидал, что такая кнопка будет зацикливаться на опции, а также инициировать событие изменения. Здесь возможно решение:
$("#fieldNext").click(function() {
if ($('#selectionChamp option:selected').next().length > 0)
$('#selectionChamp option:selected').next().attr('selected', 'selected').trigger('change');
else $('#selectionChamp option').first().attr('selected', 'selected').trigger('change');
});
Вот jsFiddle: http://jsfiddle.net/acosonic/2cg9t17j/3/