我正在尝试创建一个数组,用户在文本框中输入多个项目。所有我可以让我的功能返回是第一个条目。也许我正在接近这个错误?
function treez() {
var treesArray = [];
var ar = treesArray.push(document.getElementById("trees").value);
var st = treesArray.toString();
document.getElementById("tresult").innerHTML = st;
}
Enter 7 Trees
<br>
<textarea rows="5" cols="50" id="trees" name "trees"></textarea>
<br>
<br>
<button type="button" class="processButton" onclick="treez()">submit</button>
<br>
<p id="tresult">
<p>
答案 0 :(得分:1)
将treesArray
移出trees
功能。
随着它的存在,每次通话都会重新创建。
var treesArray = [];
function treez() {
var ar = treesArray.push(document.getElementById("trees").value);
var st = treesArray.toString();
document.getElementById("tresult").innerHTML = st;
}
&#13;
Enter 7 Trees
<br>
<textarea rows="5" cols="50" id="trees" name "trees"></textarea>
<br>
<br>
<button type="button" class="processButton" onclick="treez()">submit</button>
<br>
<p id="tresult">
<p>
&#13;
答案 1 :(得分:0)
你可以这样做,要求用户用逗号分隔每个条目:
var treesArray = []
function treez() {
var trees = document.getElementById("trees").value;
treesArray = trees.split(",");
document.getElementByID("tresult").innerHTML = treesArray +
" " + treesArray.join(" - ");
}