[英]JavaScript working fine In IE, Firefox and Opera but not in Safari or Chrome (Textbox.value)
The JavaScript code below is executed every time the user selects the login button on my webpage, the function basically ensures that the username
and password
fields have received text (and then strips away any leading or trailing white space) and then redirects the browser to the php login script. 每次用户选择我的网页上的登录按钮时,都会执行以下JavaScript代码,该功能基本上确保username
和password
字段已接收到文本(然后剥离任何前导或尾随空白区域),然后将浏览器重定向到php登录脚本。 The code works perfectly in IE, Firefox and Opera yet it doesn't in Safari and Chrome. 该代码在IE,Firefox和Opera中完美运行,但在Safari和Chrome中却没有。
In Safari and Chrome, the textbox values are undefined
. 在Safari和Chrome中,文本框值undefined
。 Other browsers return the values typed perfectly. 其他浏览器返回完美键入的值。
var button;
var name;
var pass;
function logIn()
{
name.value = name.value.replace(/^\s+|\s+$/g,"");
pass.value = pass.value.replace(/^\s+|\s+$/g,"");
if(name.value == "")
{
alert("Please Enter A Username");
}
else if(pass.value == "")
{
alert("Please Enter A Password");
}
else
{
window.location.href = "loginScript.php?username=" + name.value + "&password=" + pass.value;
}
}
function onLoad()
{
button = document.getElementById('button');
name = document.getElementById('username');
pass = document.getElementById('password');
button.onclick = logIn;
}
window.onload = onLoad;
I believe the variable you're using, name, is a reserved keyword in Chrome. 我相信您使用的变量name是Chrome中的保留关键字。
Try changing the name variable to something like username. 尝试将name变量更改为username。
var button;
var username;
var password;
function logIn()
{
username.value = username.value.replace(/^\s+|\s+$/g,"");
pass.value = pass.value.replace(/^\s+|\s+$/g,"");
if(username.value == "")
{
alert("Please Enter A Username");
}
else if(password.value == "")
{
alert("Please Enter A Password");
}
else
{
window.location.href = "loginScript.php?username=" + username.value + "&password=" + password.value;
}
}
function onLoad()
{
button = document.getElementById('button');
username = document.getElementById('username');
password= document.getElementById('password');
button.onclick = logIn;
}
window.onload = onLoad;
EDIT: Changed pass to password as well 编辑:也改为传递给密码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.