数组显示错误的东西

时间:2018-02-25 04:32:12

标签: javascript html

我希望此代码显示来自HTML页面上表单的文本输入的信息,使用数组,它会将信息转换为数组,然后将其显示回textarea,尽管每次都覆盖第一个线,没有正确显示

<div class="row" style=" margin-top:100px; " ng-app="app" ng-class="active">
<h1>Home</h1>
</div>  

1 个答案:

答案 0 :(得分:0)

问题似乎出现在这一行var arr = new Array();上。每次它都会创建一个新数组。

尝试在函数外声明数组。似乎不需要count。该值可以简单地在数组中推送

var count = 0;
var arr = [];
var input = document.forms["ShoppingForm"]["choiceTxt"].value;

function listChoice(){
    arr.push(document.forms["ShoppingForm"]["choiceTxt"].value + "\n");
    for(var i = 0; i <= arr.length; i++){
        document.forms["ShoppingForm"]["listDisplay"].value = count + ". " + 
        arr[i] + "\n";
    }
}
相关问题