出于学习目的,我正在构建一个简单的消息传递程序,我认为它可以与本地存储一起使用。关于二维数组的另一个问题是在评论中。
<!DOCTYPE HTML>
<html>
<head>
<title>waGwan?</title>
<meta charset="utf-8"/>
<link rel=stylesheet href=comm.css></link>
</head>
<body>
<section>
<p>enter or create passcode: <input type=text id=passcode></p>
<input type=button id="button" value="send">
</section>
<section id="log"></section>
<script type="text/javascript">
//initializing my passcode. Pass is a two dimensional array that holds passcodes and arrays of corresponding message logs; how can I instantiate my passcode without nil but maintain pass as a two dimensional array, just not include pass?
var pass=[mypasscode,nil];
document.getElementById("button").onclick=checkPass;
function checkPass(){
for(i=0;i<pass.length;i++){
//exits if passcode exists
if(document.getElementById("passcode").value==pass[i]){
break;
}
//if passcode doesn't equal last existing passcode the passcode and a new array (that stores the message log) is added to the pass array
else if(document.getElementById("passcode").value!==pass[pass.length-1]){
pass.push(document.getElementById("passcode").value,[We can chat here.]);
}
}
document.write("x");
}
</script>
</body>
</html>
答案 0 :(得分:0)
你有一些错误。如
pass.push(document.getElementById("passcode").value,[We can chat here.]);
[We can chat here.]
无效。
并且在初始化时放入数组的这些变量不会被声明。
var pass=[mypasscode,nil];
如果您使用的是Chrome或IE,我建议您点击f12,然后查看脚本部分,以便更好地调试问题。
尝试以下方法,虽然可能不是您想要做的事情,但仍有效。
<强> Live Demo 强>
//initializing my passcode. Pass is a two dimensional array that holds passcodes and arrays of corresponding message logs; how can I instantiate my passcode without nil but maintain pass as a two dimensional array, just not include pass?
var pass=["mypasscode","nil"];
document.getElementById("button").onclick=checkPass;
function checkPass(){
for(i=0;i<pass.length;i++){
//exits if passcode exists
if(document.getElementById("passcode").value==pass[i]){
break;
}
//if passcode doesn't equal last existing passcode the passcode and a new array (that stores the message log) is added to the pass array
else if(document.getElementById("passcode").value!==pass[pass.length-1]){
pass.push(document.getElementById("passcode").value);
}
}
document.write("x");
}
答案 1 :(得分:0)
pass.push(document.getElementById("passcode").value,[We can chat here.]);
^^^^^^^^^^^^^^^^^^^
here is the problem
试试这个
pass.push(document.getElementById("passcode").value,["We can chat here."]);
你可以像netbeans一样使用ide,它会警告你有任何错误,如下面的