简体   繁体   English

我无法在 JavaScript 中显示我的数组

[英]I can't show my array in JavaScript

I have some simple code in JavaScript.我在 JavaScript 中有一些简单的代码。 I was going to create code to add first name, last name and age of students from the client by click on "Add Student" button, and show the list of students including the one just added by clicking on the "Show Students" button.我将创建代码,通过单击“添加学生”按钮从客户端添加学生的名字、姓氏和年龄,并通过单击“显示学生”按钮显示学生列表,包括刚刚添加的学生。

The add action is successful, but when I click on "Show Students" button it just shows me a table without any information(First Name,Last Name,Age) .添加操作成功,但是当我单击“显示学生”按钮时,它只显示一个没有任何information(First Name,Last Name,Age)的表格information(First Name,Last Name,Age)

 var students = new Array(); var i = 0; function AddStudent() { var patt = /i[az]/; var Fname = prompt("enter first name"); var Lname = prompt("enter last name"); var Age = prompt("enter age"); var std= {fname : Fname,lname : Lname,age : Age } students[i++] =std; } function ShowStudents() { var table = "<table style='background-color:greenyellow; border:solid;'>"; table += "<tr><th>نام</th><th>نام خانوادگی</th><th>سن</th></tr>" for( var j=0;j<students.length;j++) { table += "<tr>" table += "<td>" + students[j].fname +"</td>" table += "<td>" + students[j].lname + "</td>" table += "<td>" + students[j].age + "</td>" table +="</tr>" } table += "</table>"; document.getElementById("ShowStudents").innerHTML = table; }
 <input type="button" value="Add Student" onclick="AddStudent();"/> <input type="button" value="Show Students" onclick="ShowStudents();"/> <div id="ShowStudents"></div>

I think that your browser can not execute instructions without " ; " , so try to add it in this lines :我认为您的浏览器无法在没有“ ; ”的情况下执行指令,因此尝试将其添加到以下行中:

table += "<tr><th>نام</th><th>نام خانوادگی</th><th>سن</th></tr>"
for( var j=0;j<students.length;j++)
    {                                                      
        table += "<tr>"
        table += "<td>" + students[j].fname +"</td>"
        table += "<td>" + students[j].lname + "</td>"
        table += "<td>" + students[j].age + "</td>"
        table +="</tr>"
    }

because for me it works like a charm.因为对我来说它就像一种魅力。

我在我的 js 页面中有一些评论和无用的功能。最后我清理了评论和无用的功能,它起作用了!

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

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