简体   繁体   English

我的代码不起作用,怎么了?

[英]My code does not work, what's wrong?

I'm new to JavaScript, but I followed a really good tutorial, and I'm making a sign in pop-up form, but it doesn't work... The idea is that you have one username, and one password, if both are correct, you will redirect to a page. 我是JavaScript的新手,但是我遵循了一个非常好的教程,并且正在弹出式窗口中进行登录,但这种方法不起作用...想法是您只有一个用户名和一个密码,如果两者都正确,您将重定向到页面。 I lost the HTML code for the form, but with an input field, the id has to be uName, and with the password field pWord. 我丢失了该表单的HTML代码,但使用输入字段时,id必须为uName,并且使用密码字段pWord。 Here's my JavaScript code: 这是我的JavaScript代码:

function myFunction(){

    var uName = document.getElementById("uName").value;
    var pWord = document.getElementById("pWord");


}


// This line controls the uName and pWord.

if(uName = "Admin",pWord = "Admin"){

// This line creates a pop-up with the name.

alert("Welcome " + uName);

// Need a line to redirect to a new page

// Need a line with the else statement

// Need a line to say if something if not all fields are filled


}

The username has to be Admin and the password Admin. 用户名必须为Admin,密码为Admin。 If you don't enter the right combination, you have to get a message like: "Incorrect combination.". 如果未输入正确的组合,则必须收到类似“错误的组合”的消息。

This is assignment with = operator : 这是=运算符的赋值:

if (uName = "Admin",pWord = "Admin") {

While you need comparison == or === : 当您需要比较=====

if (uName == "Admin" && pWord == "Admin") {

Also usage of the comma , operator is not correct (well it is correct, but not in this place). 另外,逗号,运算符的用法也不正确(嗯,这是正确的,但是在此位置不正确)。 You need logical AND && . 您需要逻辑AND &&

Assignment inside of if check makes truthy expression ("Admin" string is truthy), making code always enter if-block. if check内部的赋值使表达式为真(“ Admin”字符串为true),使代码始终输入if-block。

There's a couple of issues: 有几个问题:

  1. You're not getting the value of the password field, but the object 您没有获得密码字段的值,但是得到了
  2. You're not validating the credentials inside the function 您没有在验证函数内部的凭据
  3. You are assigning "Admin" to uName and pWord with a single = 您正在将“ Admin”分配给带有单个= uNamepWord
  4. You need to use && to match both tests of your if statement 您需要使用&&来匹配if语句的两个测试

As a side note: You should never validate a login with the username and password hardcoded like this, it is always avaiable to the user, all you would need to do is right click in browser and view source to see the username and password - this login script is fine for learning how javascript works, but don't implement a login like this in the real world unless it is purely a crawler type deterrent 附带说明:您永远不要使用这样的硬编码验证用户名和密码来验证登录名,该登录名始终可供用户使用,您所需要做的就是在浏览器中右键单击并查看源代码以查看用户名和密码-这登录脚本可以很好地学习javascript的工作原理,但是在现实世界中请勿实现这种登录方式,除非它纯粹是对爬虫类型的威慑

= sets a value, == matches the value, === matches the value and type (string, object, int) =设置值, ==匹配值, ===匹配值和类型(字符串,对象,整数)

function myFunction(){

    var uName = document.getElementById("uName").value;
    var pWord = document.getElementById("pWord").value;

    // This line controls the uName and pWord.

    if(uName == "Admin" && pWord == "Admin"){

        // This line creates a pop-up with the name.

        alert("Welcome " + uName);

        // Need a line to redirect to a new page

        // Need a line with the else statement

        // Need a line to say if something if not all fields are filled
    }

}

To combat your comments 打击您的意见

// Need a line to redirect to a new page
window.location.href="/pagetoredirectto.html";

and

// Need a line with the else statement
if(uName == "Admin" && pWord == "Admin"){

    alert("Welcome " + uName);

} else {

    alert( "User and Pass do not match" );

}

and this should go just after you get the values.. so the full code is at the bottom of this answer 并且应该在获取值之后执行..因此完整的代码在此答案的底部

// Need a line to say if something if not all fields are filled
if( !uName || !pWord )
{
    alert( "Enter both a username and password" );
    return;
}

We use a return here to stop the rest of the function from executing 我们在此处使用return来阻止其余函数执行

Full Code 完整代码

function myFunction(){

    var uName = document.getElementById("uName").value;
    var pWord = document.getElementById("pWord").value;

    // Need a line to say if something if not all fields are filled
    if( !uName || !pWord )
    {
        alert( "Enter both a username and password" );
        return;
    }

    // This line controls the uName and pWord.

    if(uName == "Admin" && pWord == "Admin"){

        alert("Welcome " + uName);

        window.location.href="/pagetoredirectto.html";

    } else {
        alert( "User and Pass do not match" );
    }

}

Change your code as below and check: 如下更改代码并检查:

function myFunction(){
    var uName = document.getElementById("uName").value;
    var pWord = document.getElementById("pWord").value;

    if(uName != "" && pWord != ""){ /* Ensure both fields have value */
        if(uName == "Admin" && pWord == "Admin"){
            alert("Welcome " + uName);
            /* Redirection code goes here */
        }else{
            /* If user is not Admin */
        }
    }else{
        /* Validation message goes here */
    }        
 }

Thats it i Hope: 就是这样,我希望:

 function myFunction(){ var uName = document.getElementById("uname").value; var pWord = document.getElementById("pWord").value; if (uName == "Admin" && pWord == "Admin") { // This line creates a pop-up with the name. alert("Welcome " + uName); // Need a line to redirect to a new page document.location = "http://www.google.de" // Need a line to say if something if not all fields are filled }else if (uName == "" || pWord == "") { alert("Fill all fields"); // Need a line with the else statement }else{ alert("Incorrect Combination"); } } 
 <input id="uname"></input> <input id="pWord"></input> <input type="button" onclick="myFunction()" value="Login"></input> 

Still a way to insecure Version of a login script. 仍然是一种不安全的登录脚本版本的方法。

If you go to debug you can see username and pass in cypher text plain insecurity. 如果您要进行调试,则可以看到用户名并以纯文本形式输入不安全的密文。

You are taking reference of particular whole object in var pWord though you intend to take only value of password field. 尽管您打算仅使用密码字段的值,但是您正在引用var pWord中特定的整个对象。

Also You are assigning "Admin" to uName and pWord with the single = . 另外,您还使用单个=分配“ Admin”给uNamepWord For comparison use === or == though I would recommend use of === . 为了进行比较,请使用=====尽管我建议使用===

Do this: 做这个:

function myFunction(){
    var uName = document.getElementById("uName").value;
    var pWord = document.getElementById("pWord").value;
}

For redirection use 进行重定向使用

window.location = "http://www.location.com/ie.htm";

Well try this example: 好吧,请尝试以下示例:

function myFunction(){
    var uName = document.getElementById("uName").value;
    var pWord = document.getElementById("pWord").value;

    // This line controls the uName and pWord.
    if(uName == "Admin" && pWord == "Admin"){
        // This line creates a pop-up with the name.
        alert("Welcome " + uName);
        // Need a line to redirect to a new page
        location.href = "http://www.SOMEURL.com";
        // Need a line with the else statement
    } else {
        // Need a line to say if something if not all fields are filled
        alert("Wrong input");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM