当前,我正在尝试模拟登录/注册表单,并且当前遇到一个问题,我似乎找不到解决方法来解决该问题,该问题表明我的“用户名”未定义但正在运行{{1 }}首先运行。
onReg
我认为可能是因为它不是全球范围?如果可以的话,如何用这段代码来实现呢?
答案 0 :(得分:1)
username
内的 onLogin
是形式函数参数。调用onLogin
时,必须显式传递一个值。
var x = 'foo';
function checkX() {
alert(x);
}
function badCheckX(x) {
alert(x);
}
checkX(); // 'foo'
badCheckX(); // undefined
badCheckX('bar'); // 'bar'
badCheckX(x); // 'foo'
答案 1 :(得分:0)
我认为您应该使用document.getElementById("[name=username]").value
答案 2 :(得分:0)
请勿使用已经声明为函数的参数的变量。 username
和password
已被声明为变量,因此您应使用其他名称替换它们。尝试改用usernameX
和passwordX
。
function onLogin(usernameX,passwordX) {
// will alert the value of the first parameter (usernameX)
alert("Does this work?" + usernameX)
//if you want to get the value of the variable
alert("Does this work?" + username)
}