<!DOCTYPE html>
<html>
<head>
<!--import the external class file-->
<script src="Student.js"></script>
<script>
var studentArray = [];
var professorArray = new Array();
//when register button is clicked, add a user to array
function addUser(email, date, content){
var email = document.getElementById("email").value;
var extension = getExtension(email);
if(extension == "mylaurier.ca"){
//identified as a student, create and push the student into the array
var student = new Student(email, 0, "");
studentArray.push(student);
}
}
//get the extension of the user email
function getExtension(email){
var elements = email.split("@");
return elements[1];
}
</script>
</head>
<body>
<form>
<label for="email">Email:</label>
<input type="text" name="email" id="email" placeholder="Enter Email" value="" data-clear-btn="true">
<button onclick="addUser()">Register</button>
</form>
</body>
</html>
想将不同的对象推入数组,但实际上会导致覆盖,数组中总有1个对象,这是推送的最后一个对象
答案 0 :(得分:0)
点击注册按钮表单提交时,会再次重新加载页面。所以 studentArray 再次初始化。
设置 onsubmit =&#34;为表单返回false&#34; 。
试试这段代码。
<form onsubmit="return false">
<label for="email">Email:</label>
<input type="text" name="email" id="email" placeholder="Enter Email" value="" data-clear-btn="true">
<button onclick="addUser()">Register</button>
</form>
希望这会对你有所帮助。
答案 1 :(得分:-2)
尝试
var studentArray = new Array;
而不是
var studentArray = [];