我的代码不起作用,出了什么问题?

时间:2014-12-11 09:41:54

标签: javascript html forms popup

我是JavaScript的新手,但我遵循了一个非常好的教程,我在弹出窗口中签名,但它不起作用......我的想法是你有一个用户名和一个密码,如果两者都正确,你将重定向到一个页面。我丢失了表单的HTML代码,但是输入字段的id必须是uName,密码字段是pWord。这是我的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


}

用户名必须是Admin,密码为Admin。如果您没有输入正确的组合,则必须收到如下消息:"组合不正确。"。

6 个答案:

答案 0 :(得分:3)

这是= operator的作业:

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

虽然您需要比较== or ===

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

同样使用逗号, operator是不正确的(这是正确的,但不是在这个地方)。您需要逻辑AND &&

if检查内部的分配使得表达式(" Admin"字符串是真实的),使代码始终输入if-block。

答案 1 :(得分:3)

有几个问题:

  1. 您没有获得密码字段的值,而是获取对象
  2. 您未验证功能
  3. 中的凭据
  4. 您正在分配"管理员"使用单个uName
  5. pWord=
  6. 您需要使用&&来匹配if语句的两个测试
  7. 作为旁注:您永远不应该使用像这样硬编码的用户名和密码验证登录,它始终可供用户使用,您需要做的就是右键单击浏览器并查看源代码以查看用户名和密码 - 这个登录脚本可以用于学习javascript的工作方式,但不要在现实世界中实现这样的登录,除非它纯粹是一种爬虫类型的威慑

    =设置一个值,==匹配值,===匹配值和类型(字符串,对象,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
        }
    
    }
    

    打击你的评论

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

    // Need a line with the else statement
    if(uName == "Admin" && pWord == "Admin"){
    
        alert("Welcome " + uName);
    
    } else {
    
        alert( "User and Pass do not match" );
    
    }
    

    这应该在你获得值之后才进行..所以完整的代码就在这个答案的底部

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

    我们在这里使用return来阻止函数的其余部分执行

    完整代码

    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" );
        }
    
    }
    

答案 2 :(得分:1)

如下所示更改您的代码并检查:

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 */
    }        
 }

答案 3 :(得分:1)

多数民众赞成我希望:



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>
&#13;
&#13;
&#13;

仍然是一种不安全的登录脚本版本的方法。

如果你去调试,你可以看到用户名并传递密码短文不安全。

答案 4 :(得分:0)

您正在参考var pWord中的特定整个对象,但您打算只取密码字段的值。

此外,您使用单个uName为“{1}}和pWord分配”管理员“。为了进行比较,请使用====,但建议使用==

这样做:

===

使用重定向

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

答案 5 :(得分:0)

试试这个例子:

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");
    }
}