简体   繁体   English

我需要以表格格式打印数组,并能够添加带有值的新行

[英]I need to print array in table format and be able to add new row with value

I have issue when i press ADD button it does not push value = 0 into array and wouldn't print it out, I'm just getting into coding. 当我按下ADD按钮时,我没有将值= 0推入数组并且不会打印出来的问题,我只是在编码。

<button id="add">ADD</button>
<p id="demo"></p>

<script>
var fruits, text, fLen, i;
fruits = ["0", "1", "2", "3"];

function myFunction() {
 fruits.push("0");
}

function print() {
fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";

}

window.onload = print();
document.getElementById("demo").innerHTML = text;
document.getElementById("add").addEventListener("click", myFunction);

</script>

Your code is correct, it pushes new value to array. 您的代码是正确的,它将新值推送到数组。 But your print function that prints array values gets triggered only on window load. 但是,仅在窗口加载时才触发打印数组值的打印函数。

So you should clear the old print, print it again, once you push a new value 因此,一旦按下新的值,就应该清除旧的打印件,然后再次打印

  var fruits, text, fLen, i; fruits = ["0", "1", "2", "3"]; function myFunction() { fruits.push("0"); console.log(fruits); print(); } function print() { fLen = fruits.length; text = "<ul id='printed-array'>"; for (i = 0; i < fLen; i++) { text += "<li>" + fruits[i] + "</li>"; } text += "</ul>"; //I have put this line within print() function scope, so it gets updated with new text variable value document.getElementById("demo").innerHTML = text; } window.onload = print(); document.getElementById("add").addEventListener("click", myFunction); 
 <button id="add">ADD</button> <p id="demo"></p> 

var fruits, text, fLen, i;
fruits = ["0", "1", "2", "3"];

function myFunction() {
 fruits.push("0");
 document.getElementById("demo").innerHTML = print(); // added this to get new value doing print 
}

function print() {
fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
return text; // also added this to get return;
}

window.onload = print();
document.getElementById("demo").innerHTML = text;
document.getElementById("add").addEventListener("click", myFunction);

Your function was fine except get new value i added return on your print() function to get new value then call it when you push new value 除了获得新值外,您的函数还不错,我添加了print()函数的返回值以获取新值,然后在您推送新值时调用它

You're pushing the value but not printing it. 您在推送值,但没有打印出来。 You need to call the print() method after pushing the value and change the innerHTML. 您需要在推入值并更改innerHTML之后调用print()方法。

Your function should be 你的功能应该是

function myFunction() {
 fruits.push("0");
 print();
 document.getElementById("demo").innerHTML = text;
}

Fiddle: https://jsfiddle.net/Lgkzsy41/ 小提琴: https//jsfiddle.net/Lgkzsy41/

var fruits, fLen, i;
fruits = ["0", "1", "2", "3"];

function myFunction() {
 fruits.push("0");
 print();
}

function print() {
   var text='<ul>';
   fLen = fruits.length;            
   for (i = 0; i < fLen; i++) {
     text += "<li>" + fruits[i] + "</li>";
   }   
   text += '</ul>'
   document.getElementById("demo").innerHTML = text;
}

window.onload = print();
document.getElementById("add").addEventListener("click", myFunction);

Complete example jsfiddle 完整的示例jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM