使用JavaScript根据输入字段更改href标记

时间:2018-12-12 15:00:57

标签: javascript html

我想根据密码是否正确来更改此链接。我想设置一个密码,我只知道html和最小的JS。我想我已经设置好了,所以当密码是wima时,它将更改href并允许链接工作。不会的我可以帮忙吗?

function login()
var password = getElementById("password"); {
  if (password = "wima") {
    getElementById("submit").href = "/pages/home.html";
  } else {
    getElementById("submit").href = "index.html";
  }
}
<p>

  Username
  <input id="username" type=text placeholder="WIMA"><br> Password
  <input id="password" type=password placeholder="WIMA"><br>

  <a class="button" id="submit" href="#" onclick="login()">
    Submit 
    </a>
</p>

5 个答案:

答案 0 :(得分:1)

这里发生了几件事:

  • <input>访问.value中的值;
  • 您放错了{
  • getElementById不是全局方法,必须在要选择的元素上(在您自己的情况下为文档本身)调用它
  • 要测试两个值是否相等,请在js中使用===

function login() {
  var password = document.getElementById("password").value;
  if (password === "wima") {
    document.getElementById("submit").href = "/pages/home.html";
  } else {
    document.getElementById("submit").href = "index.html";
  }
}
<p>

  Username
  <input id="username" type="text" placeholder="WIMA"><br> Password
  <input id="password" type="password" placeholder="WIMA"><br>

  <a class="button" id="submit" href="#" onclick="login()">
    Submit 
    </a>
</p>

答案 1 :(得分:1)

JavaScript有一些问题。

<script language="JavaScript">

function login()
var password = getElementById("password"); // this gets the element, not the value of the element
{ // this curly brace is in the wrong place
if (password = "wima") { // this sets the value of the password var to "wima" 
getElementById("submit").href="/pages/home.html";
}
else {
getElementById("submit").href="index.html";
}
} 
</script>

这是您的代码,已清理。

<script language="JavaScript">

function login() {
    var password = document.getElementById("password").value;
    if (password == "wima") { // use == to compare value
        document.getElementById("submit").href="/pages/home.html"; 
    }
    else {
        document.getElementById("submit").href="index.html";
    }
}
</script>

这里的另一个问题是,您不应更改用于执行login()函数的元素的href。

您可以像这样将用户重定向到新页面:

   <script language="JavaScript">

    function login() {
        var password = document.getElementById("password").value;
        if (password == "wima") { 
            window.location.href="/pages/home.html";
        }
        else {
            window.location.href="index.html";
        }
    }
    </script>

答案 2 :(得分:0)

如果您要基于输入类型文本更改href值,我想您做错了。您应该对密码输入文本进行模糊/更改事件。根据用户单击href时的密码值,应相应地将其重定向。 检查一下:

function login() {
        var _password = document.getElementById("password").value;
        if ("wima" == _password) {
            document.getElementById("submit").href = "/pages/home.html";
        } else {
            document.getElementById("submit").href = "index.html";
        }
    }
<p>

        Username
        <input id="username" type=text placeholder="WIMA">
        <br> Password
        <input id="password" type=password placeholder="WIMA" onblur="login()">
        <br>

        <a class="button" id="submit" href="#">
    Submit 
    </a>
    </p>

答案 3 :(得分:0)

这是带有开关的表单验证器。

function validateForm() {
  var x = document.forms["myForm"]["password"].value;

  switch (x) {
    case "":
      alert("Name must be filled out");
      return false;
      break;

    case "wima":
      return true;
      break;

    default:
      alert("Error: Wrong Password.");
      document.location.href = "https://stackoverflow.com/search?q=notloggedin";
      // Replace the link above with your error link return
      return false;
  }

}
<!-- Replace action link with your successful link -->
<form name="myForm" action="https://stackoverflow.com/search?q=login" onsubmit="return validateForm()" method="post">
  <input type="password" name="password">
  <input type="submit" value="Submit">
</form>

答案 4 :(得分:0)

如果有人检查html / javascript,您的密码将显示在文本中。因此,不建议采用这种安全方法。对于基本概念,基于输入进行链接更改很有趣。试试这个。

<p>

  Username
  <input id="username" type="text" placeholder="WIMA"><br> Password
  <input id="password" type="password" placeholder="WIMA"><br>

  <a class="button" id="submit" >
    Submit 
    </a>
</p>
<script>
    var password = document.getElementById('password');
    password.addEventListener('change', enableLogin);
    var submit = document.getElementById('submit');

    function enableLogin() {
      if (password.value == "wima") { // it is an easy mistake (= or ==)
        submit.href = "/pages/home.html";
      } else {
        submit.removeAttribute('href');
      }
    }
</script>