Javascript 下一步按钮,使用js选择下拉菜单的下一个选项 1. 2. 3. 下一个 var select2=document.getElementById(“表单”); var lastselected2=localStorage.getItem('select2'); document.getElementById('next') .addEventListener('单击',函数()){ lastselected2=parseInt(lastselected2)+1; }); 如果(上次选择2){ select2.value=lastselected2; } 选择2.onchange=函数(){ lastselected2=select2.options[select2.selectedIndex]。值; console.log(lastselected2); setItem('select2',lastselected2); }

Javascript 下一步按钮,使用js选择下拉菜单的下一个选项 1. 2. 3. 下一个 var select2=document.getElementById(“表单”); var lastselected2=localStorage.getItem('select2'); document.getElementById('next') .addEventListener('单击',函数()){ lastselected2=parseInt(lastselected2)+1; }); 如果(上次选择2){ select2.value=lastselected2; } 选择2.onchange=函数(){ lastselected2=select2.options[select2.selectedIndex]。值; console.log(lastselected2); setItem('select2',lastselected2); },javascript,html,Javascript,Html,我有一个保存下拉列表中所选内容的代码,我想添加一个按钮,可以选择下拉列表的下一个选项。试试这个 <select id="form"> <option value="1" >1 </option> <option value="2" >2 </option> <option value="3" >3 </option> </select> <button id='next'

我有一个保存下拉列表中所选内容的代码,我想添加一个按钮,可以选择下拉列表的下一个选项。

试试这个

<select id="form">
    <option value="1" >1 </option>
    <option value="2" >2 </option>
    <option value="3" >3 </option>
</select>
<button id='next'>Next</button>
<script>
    var select2 = document.getElementById("form");
    var lastselected2 = localStorage.getItem('select2');
    document.getElementById('next')
            .addEventListener('click', function(){
                  lastselected2 = parseInt(lastselected2) + 1;
            });

    if(lastselected2) {
        select2.value = lastselected2; 
    }

    select2.onchange = function () {
        lastselected2 = select2.options[select2.selectedIndex].value;
        console.log(lastselected2);
        localStorage.setItem('select2', lastselected2);
    }
</script>
又快又脏我知道…


var button = document.getElementById('next');
button.onclick = function() {
   select2.value = parseInt(lastselected2)+1;
}
函数selectNext(){ var select=document.getElementById('form'); select.selectedIndex++; }
Next

这是一个很好的选择,可以找到所选的实际索引并按索引直接转到下一个索引


好的。有问题吗?嗨,邓扎!添加的位置不保存是什么意思?您使用的浏览器是什么?对我来说,它保存在chrome中。chrome,在页面重新加载时,它只返回先前保存的选项Hi Denza,要保存状态,在所有上下文中,您将始终需要一些cookie或本地存储
<script>
function selectNext(){
  var select = document.getElementById('form');
  select.selectedIndex++;
}
</script>