这段JavaScript代码有什么问题? [英] What's wrong in this JavaScript code?
本文介绍了这段JavaScript代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标记:
<形式>
< fieldset>< p>
< label>用户名:< input type =textname =userid =user/>< / label>< / p>< br />< p> ;
< label>密码:< input type =passwordname =passwid =passw/>< / label>< / p>
< label>< input type =submitvalue =登录name =submitBUTstyle =width:65px; height:25px; background-color:green; color:white; font-重量:粗体; id =submitBUT/>< div id =helllo>< / div>< / label>
< / fieldset>
< / form>
代码:
var subBut = document.getElementById('submitBUT');
var users = ['hithere','Peter112','Geksj','lOsja','fInduS','323DssaAA','f1fsus'];
var passes = ['mllesl','Petboy','Heneeesh','Olga','234dasdf77 /','minls','ew832ja'];
var output = [];
函数submi(u,p){
for(var i = 0; i< users.length; i ++){
if(users [i] == u){
output.push(i);
for(var o = 0; o< output.length; o ++){
if(passes [output [o]] == p){
返回p +'是一个正确的密码。';
}
}
返回'错误,请重试';
}
subBut.onclick =(document.getElementById('helllo')。innerHTML =(submi(document.getElementById('user')。value,document.getElementById('passw')。value) ));
LIVE CODE; http://jsfiddle.net/w87MS/
是的,我知道你不会在源代码中存储密码:D,它只是毛皮t3h lulz
解决方案
我必须同意上面的评论,不知道你在做什么,但是......问题在于 submit
处理程序是一个函数而不是字符串:
$ b
subBut.onclick =(document.getElementById '')。innerHTML =(submi(document.getElementById('user')。value,document.getElementById('passw')。value)));
应该是:
subBut.onclick = function(){
document.getElementById('helllo')。innerHTML =
submi(document.getElementById('user').value,document.getElementById 'PASSW')值);
返回false; //用于测试,防止表单提交
};
I can't figure it out..
Markup:
<form>
<fieldset><p>
<label>Username: <input type="text" name="user" id="user" /></label></p><br /><p>
<label>Password: <input type="password" name="passw" id ="passw" /></label></p>
<label><input type="submit" value="Log in" name="submitBUT" style="width: 65px; height: 25px; background-color: green; color: white; font-weight: bold;" id="submitBUT" /><div id="helllo"></div></label>
</fieldset>
</form>
Code:
var subBut = document.getElementById('submitBUT');
var users = ['hithere', 'Peter112', 'Geksj', 'lOsja', 'fInduS', '323DssaAA', 'f1fsus'];
var passes = ['mllesl', 'Petboy', 'Heneeesh', 'Olga', '234dasdf77/', 'minls', 'ew832ja'];
var output = [];
function submi(u, p) {
for (var i = 0; i < users.length; i++) {
if (users[i] == u) {
output.push(i);
}
}
for (var o = 0; o < output.length; o++) {
if (passes[output[o]] == p) {
return p + ' was a correct password.';
}
}
return 'Error, please try again';
}
subBut.onclick = (document.getElementById('helllo').innerHTML=(submi(document.getElementById('user').value, document.getElementById('passw').value)));
LIVE CODE; http://jsfiddle.net/w87MS/
and yes, I know you don't store passwords in the source code :D, it's just "fur t3h lulz"
解决方案
I have to agree with comments above, not sure what you're doing but...the problem is that a submit
handler is a function not the string you're assigning, this:
subBut.onclick = (document.getElementById('helllo').innerHTML=(submi(document.getElementById('user').value, document.getElementById('passw').value)));
should be:
subBut.onclick = function() {
document.getElementById('helllo').innerHTML=
submi(document.getElementById('user').value, document.getElementById('passw').value);
return false; //for testing, prevent form submission
};
You can test the updated version here.
这篇关于这段JavaScript代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文