简体   繁体   English

JavaScript工作正常在IE,Firefox和Opera中,但在Safari或Chrome中没有(Textbox.value)

[英]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代码,该功能基本上确保usernamepassword字段已接收到文本(然后剥离任何前导或尾随空白区域),然后将浏览器重定向到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.

相关问题 Javascript / jQuery无法在Firefox,Safari和IE中运行。 精通Opera和Chrome - Javascript/jQuery not working in Firefox, Safari and IE. Fine in Opera and Chrome CSS无法在IE,Firefox,Opera中加载,并且无法与Chrome和Safari正常运行 - Css not loading in IE,Firefox,Opera and working fine with Chrome and Safari JavaScript代码仅在Chrome中有效,而在Firefox,IE,Opera和Safari中无效 - JavaScript code working only in Chrome but not in Firefox, IE, Opera and Safari Ajax 调用/javascript - 在 IE、Firefox 和 Safari 上运行良好,但在 Chrome 上运行不正常 - Ajax call/javascript - working fine on IE, Firefox and Safari but not on Chrome Javascript年龄门无法在Chrome上运行,在Firefox,IE,Safari中可以正常使用 - Javascript age gate not working on Chrome, fine in Firefox, IE, Safari javascript在chrome / opera / IE中不起作用,但是firefox非常好! - javascript not working in chrome/opera/IE but firefox is excellent! jQuery .css()在IE 6,7,8和Firefox中不起作用,但在Chrome,Safari,Opera中起作用 - jQuery .css() not working in IE 6,7,8 and Firefox, but works in Chrome, Safari, Opera javascript math.round和math.floor可以在IE和Opera中正常运行,但不能在Chrome,Safari或Firefox中正常运行。 得到NaN - javascript math.round and math.floor work fine in IE and Opera but not Chrome, Safari or Firefox. Getting NaN JavaScript无法在IE和Firefox中运行,在Chrome中运行良好 - JavaScript not working in IE and Firefox, works fine in Chrome 在IE 11中缩小图像质量很差 - 在Firefox,Chrome,Opera和Safari中都很好 - Downsizing Image Quality Poor In IE 11 - Fine in Firefox, Chrome, Opera and Safari
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM