如何使用javascript,jquery或其他一些聪明的方式提交下一个选项


How to submit the next option in a select with javascript,jquery, or some other clever way?

我有一个php页面,我想在我的选择中添加下一个选择功能。

这是我的代码:

<form action='' method='GET' id='searchSelect'>
   <select name='search'>
      <option>beer</option>
      <option>chips</option>
      <option selected>BACON</option>
      <option>CHEESE</option>
      <option>poutine</option>
   </select>
   <input type='submit'>
   <button>NEXT SELECTION</button>
</form>    

我觉得我应该能够聚焦选定的(在本例中为培根)并使用带有按钮的 JavaScript 提交它(奶酪),但我不确定如何正确实现这一点。任何建议将不胜感激。

这里的小提琴

编辑:澄清我的问题:我的选择中有一个很长的列表,在浏览选择时,重新选择选项栏并选择列表中的下一个选项很痛苦。我希望用户能够单击下一步,它将在当前 SELECTED 选项之后提交列表中的下一个。该页面是 php,单击时它会使用 GET 方法提交到当前页面。当页面加载时,它会抓取 GET(name=search)并将其设置为选中,并在页面上使用一些 php。

使用 jQuery(简化):

$("select").find("option:selected").next().prop("selected",true);

请参阅小提琴(e.preventDefault()显示值已更改 - 提交删除该行)

更新

使用:selected而不是[selected]来获取当前选择的选项,而不是原始选择。.

更新的小提琴

您可以在任何需要的地方对此进行编码,在这种情况下,请在提交时

JavaScript:

function selectNext(){
  document.getElementById("mySelect").selectedIndex = document.getElementById("mySelect").selectedIndex + 1;
}

.HTML:

<button onclick="selectNext()">Next</button> 

纯JavaScript代码。