【发布时间】:2019-07-30 01:28:35
【问题描述】:
当我第一次从第二个选择菜单中选择两个选项时,数组将填充这两个选项。我想要的是第二个选择的选项替换第一个,所以即使我从第二个选择菜单中选择两个选项开始,数组的长度将保持为一个,在这些选择之间动态变化。我希望你明白。谢谢你的帮助。我知道我可以让它成为一个功能,这个问题不会存在,但我无法使用它。
var select1 = document.getElementById('select1');
var select2 = document.getElementById('select2');
var array = []
function myFunct1() {
var one = select1.options[select1.selectedIndex].value;
array.splice(0, 1, one);
console.log(array);
}
function myFunct2() {
var two = select2.options[select2.selectedIndex].value;
array.splice(1, 1, two);
console.log(array);
}
<select id = 'select1' onchange = 'myFunct1()'>
<option disabled selected value> -- select an option -- </option>
<option value = 'Dog'>ONE</option>
<option value = 'Cat'>TWO</option>
<option value = 'Bear'>THREE</option>
</select>
<select id = 'select2' onchange = 'myFunct2()'>
<option disabled selected value> -- select an option -- </option>
<option value = 'Dog'>ONE</option>
<option value = 'Cat'>TWO</option>
<option value = 'Bear'>THREE</option>
</select>
【问题讨论】:
-
如果按下
array = ["One","Two"]和TWO会发生什么。数组应更改为["Two","One"]或保持["One","Two"] -
对,但我的问题是按两次时出现的。
-
您希望保留数组。
["One","Two"]还是改成这个["Two","One"]?
标签: javascript html arrays list slice