可能重复:
在javascript中按值复制数组
我是 javascript 的初学者,所以我非常感谢任何帮助或建议。我正在尝试获取我使用输入数组收集的值(由用户在文档中放入表单)以将它们设置为单词数组。所以,我想等inputs[0] = words[0]
。inputs[1] = words[1]
我想通过将单词设置为一个空数组,然后等于输入的索引值,我可以实现这一点,但它不起作用。单词一直显示为“未定义”。
function goMad() {
var words = [];
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length - 1; i++) {
inputs[i].value = words;
}
var story = words[0] + "! he said " + words[1] + " as he jumped into his convertible " + words[2] + " and drove off with his " + words[3] + " wife.";
document.getElementById("story").innerHTML = story;
console.log(words[0]);
}