如何使用2个按钮迭代jQuery UI selectmenu?

时间:2016-04-29 13:13:29

标签: javascript jquery jquery-ui select-menu jquery-ui-selectmenu

请尝试the jsFiddle with jQuery UI selectmenu and 2 buttons

借助2个按钮prevGamenextGame,我可以更改跟踪当前所选游戏编号的selectedIndex变量。

遗憾的是,jQuery UI selectmenu doc没有解释如何设置获取(以便我可以更新范围currGame)当前所选项:

screenshot

请解释:如何在jQuery UI selectmenu中设置和获取所选项目?

HTML码:

<form>
  <select name="games" id="games"></select>
  <button id="prevGame">&lt;</button>
  <span id="currGame">Loading...</span>
  <button id="nextGame">&gt;</button>
</form>

JavaScript的代码:

var yourGames = [1, 3, 5];
var hisGames = [8, 10, 12, 14];
var selectedIndex = 0;

$("#games").selectmenu();

// emulate repeating server responses
setInterval(function() {
  updateMenu();
}, 5000);

$('#prevGame').button().click(function(e) {
  e.preventDefault();
  selectedIndex = Math.max(selectedIndex - 1, 0);
  updateButtons();
});

$('#nextGame').button().click(function(e) {
  e.preventDefault();
  selectedIndex = Math.min(selectedIndex + 1, lastIndex());
  updateButtons();
});

function lastIndex() {
  return yourGames.length + hisGames.length - 1;
}

function updateButtons() {
  $('#currGame').html('selectedIndex=' + selectedIndex); // TODO: change to "Game #"
  $('#prevGame').button(selectedIndex == 0 ? "disable" : "enable");
  $('#nextGame').button(selectedIndex == lastIndex() ? "disable" : "enable");
}

function updateMenu() {
  var yourGroup = ['<optgroup label="YOUR TURN">'];
  for (var i = 0; i < yourGames.length; i++) {
    var gameNumber = yourGames[i];
    var selectedTag = (i == selectedIndex ? 'selected="selected"' : '');
    yourGroup.push(
      '<option ' +
      selectedTag +
      ' value="' +
      gameNumber +
      '">Game #' +
      gameNumber +
      '</option>');
  }
  yourGroup.push('</optgroup>');

  var hisGroup = ['<optgroup label="HIS TURN">'];
  for (var i = 0; i < hisGames.length; i++) {
    var gameNumber = hisGames[i];
    var selectedTag = (i - yourGames.length == selectedIndex ? 'selected="selected"' : '');
    hisGroup.push(
      '<option ' +
      selectedTag +
      ' value="' +
      gameNumber +
      '">Game #' +
      gameNumber +
      '</option>');
  }
  hisGroup.push('</optgroup>');

  $("#games").selectmenu('destroy')
    .empty()
    .append(yourGroup.length > 2 ? yourGroup.join('') : '')
    .append(hisGroup.length > 2 ? hisGroup.join('') : '')
    .selectmenu(); // TODO: select the game at selectIndex
}

更新

我已使用selectmenu("refresh")代替selectmenu("destroy")准备了a newer jsFiddle,但仍有一些问题。

3 个答案:

答案 0 :(得分:1)

这可以通过更好的方式完成,但以下代码提供了您的要求:

var yourGames = [1, 3, 5];
var hisGames = [8, 10, 12, 14];
var selectedIndex = 0;

$("#games").selectmenu();

$('#prevGame').button().click(function(e) {
  e.preventDefault();
  selectedIndex = Math.max(selectedIndex - 1, 0);
  updateMenu();
  updateButtons();
});

$('#nextGame').button().click(function(e) {
  e.preventDefault();
  selectedIndex = Math.min(selectedIndex + 1, lastIndex());
  updateMenu();
  updateButtons();
});

function lastIndex() {
  return yourGames.length + hisGames.length - 1;
}

function updateButtons() {
    var selectedText = $("#games option:selected").text();
  $('#currGame').html(selectedText);
  $('#prevGame').button(selectedIndex == 0 ? "disable" : "enable");
  $('#nextGame').button(selectedIndex == lastIndex() ? "disable" : "enable");
}

function updateMenu() {
  var yourGroup = ['<optgroup label="YOUR TURN">'];
  for (var i = 0; i < yourGames.length; i++) {
    var gameNumber = yourGames[i];
    var selectedTag = (i == selectedIndex ? 'selected="selected"' : '');
    yourGroup.push(
      '<option ' +
      selectedTag +
      ' value="' +
      gameNumber +
      '">Game #' +
      gameNumber +
      '</option>');
  }
  yourGroup.push('</optgroup>');

  var hisGroup = ['<optgroup label="HIS TURN">'];
  for (var i = 0; i < hisGames.length; i++) {
    var gameNumber = hisGames[i];
    var selectedTag = (yourGames.length + i == selectedIndex ? 'selected="selected"' : '');
    hisGroup.push(
      '<option ' +
      selectedTag +
      ' value="' +
      gameNumber +
      '">Game #' +
      gameNumber +
      '</option>');
  }
  hisGroup.push('</optgroup>');

  console.log(yourGroup);
  console.log(hisGroup);

  $("#games").selectmenu('destroy')
    .empty()
    .append(yourGroup.length > 2 ? yourGroup.join('') : '')
    .append(hisGroup.length > 2 ? hisGroup.join('') : '')
    .selectmenu();
}

我还更新了您的小提琴,以便您可以玩它。 https://jsfiddle.net/q07uarwr/35/

答案 1 :(得分:1)

我试了一下,得到了更新和显示文字。但是,接下来需要找到的唯一事情是,如果选择的索引更改为下拉列表中的下一个<option>组,则如何设置值。

这是主要的变化:

function updateButtons() {
  var gamesOptions = $('#games option');
  $('#currGame').html("<span>" + $(gamesOptions[selectedIndex]).text() + "</span>");
  $("#games").val(selectedIndex).change();
  $('#prevGame').button(selectedIndex == 0 ? "disable" : "enable");
  $('#nextGame').button(selectedIndex == lastIndex() ? "disable" : "enable");
  updateMenu();
}

https://jsfiddle.net/q07uarwr/34/

答案 2 :(得分:1)

jQuery和jQuery UI无法直接设置选择菜单的选定索引。您可以使用纯JavaScript方式设置所选索引。此外,我假设您希望每次选择菜单更改时更改按钮之间的文本。你可以这样做:

&#13;
&#13;
var yourGames = [1, 3, 5];
var hisGames = [8, 10, 12, 14];
var selectedIndex = 0;

setInterval(function() {
  updateMenu();
  updateCurrentGame();
  updateButtons();
}, 5000);

$("#games").selectmenu();

$('#prevGame').button().click(function(e) {
  e.preventDefault();
  selectedIndex = Math.max(selectedIndex - 1, 0);
  updateButtons();
  updateCurrentGame();
});

$('#nextGame').button().click(function(e) {
  e.preventDefault();
  selectedIndex = Math.min(selectedIndex + 1, lastIndex());
  updateButtons();
  updateCurrentGame();
});

function lastIndex() {
  return yourGames.length + hisGames.length - 1;
}

function updateButtons() {
  $('#prevGame').button(selectedIndex == 0 ? "disable" : "enable");
  $('#nextGame').button(selectedIndex == lastIndex() ? "disable" : "enable");
}

// Update the select menu when prev & next buttons are pressed
function updateCurrentGame() {
  var selectedText = $($("select#games option")[selectedIndex]).text();
  $('#currGame').html(selectedText);
  // pure js vay to set selected index
  $("#games")[0].selectedIndex = selectedIndex;
  $("#games").selectmenu("refresh");
}

// Update the selected index every time the select menu is changed manually
$("#games").on("selectmenuchange", function(e, ui) {
  console.log(ui);
  selectedIndex = ui.item.index;
  var selectedText = ui.item.element.text();
  $('#currGame').html(selectedText);
  updateButtons();
})

function updateMenu() {
  var yourGroup = ['<optgroup label="YOUR TURN">'];
  for (var i = 0; i < yourGames.length; i++) {
    var gameNumber = yourGames[i];
    var selectedTag = (i == selectedIndex ? 'selected="selected"' : '');
    yourGroup.push(
      '<option ' +
      selectedTag +
      ' value="' +
      gameNumber +
      '">Game #' +
      gameNumber +
      '</option>');
  }
  yourGroup.push('</optgroup>');

  var hisGroup = ['<optgroup label="HIS TURN">'];
  for (var i = 0; i < hisGames.length; i++) {
    var gameNumber = hisGames[i];
    var selectedTag = (yourGames.length + i == selectedIndex ? 'selected="selected"' : '');
    hisGroup.push(
      '<option ' +
      selectedTag +
      ' value="' +
      gameNumber +
      '">Game #' +
      gameNumber +
      '</option>');
  }
  hisGroup.push('</optgroup>');

  $("#games").selectmenu('destroy')
    .empty()
    .append(yourGroup.length > 2 ? yourGroup.join('') : '')
    .append(hisGroup.length > 2 ? hisGroup.join('') : '')
    .selectmenu();
}
&#13;
button#prevGame,
span#currGame,
button#nextGame,
button#newGame {
  vertical-align: top;
}
select#games {
  width: 300px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" />

<form>
  <select name="games" id="games"></select>
  <button id="prevGame">&lt;</button>
  <span id="currGame">Loading...</span>
  <button id="nextGame">&gt;</button>
</form>
&#13;
&#13;
&#13;