Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript根据输入字段更改href标记_Javascript_Html - Fatal编程技术网

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

使用JavaScript根据输入字段更改href标记,javascript,html,Javascript,Html,我想根据密码是否正确进行此更改中的链接。我想设置一个密码,我只知道html和最小JS。我想我已经设置好了,这样当密码为wima时,它将更改href并允许链接工作。那不会发生。我能得到一些帮助吗 函数登录() var password=getElementById(“密码”);{ 如果(密码=“wima”){ getElementById(“提交”).href=“/pages/home.html”; }否则{ getElementById(“提交”).href=“index.html”; } }

我想根据密码是否正确进行此更改中的链接。我想设置一个密码,我只知道html和最小JS。我想我已经设置好了,这样当密码为wima时,它将更改href并允许链接工作。那不会发生。我能得到一些帮助吗

函数登录()
var password=getElementById(“密码”);{
如果(密码=“wima”){
getElementById(“提交”).href=“/pages/home.html”;
}否则{
getElementById(“提交”).href=“index.html”;
}
}

用户名

密码

这里发生了一些事情:

  • 中的值由
    .value
    访问
  • 你把
    {
  • getElementById
    不是一个全局方法,必须对要在其中选择的元素(在您的情况下是文档本身)调用它
  • 要测试两个值是否相等,请在js中使用
    ==
函数登录(){
var password=document.getElementById(“密码”).value;
如果(密码==“wima”){
document.getElementById(“提交”).href=“/pages/home.html”;
}否则{
document.getElementById(“提交”).href=“index.html”;
}
}

用户名

密码

您的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>

函数登录()
var password=getElementById(“password”);//这将获取元素,而不是元素的值
{//这个大括号放错地方了
如果(password=“wima”){//这将密码变量的值设置为“wima”
getElementById(“提交”).href=“/pages/home.html”;
}
否则{
getElementById(“提交”).href=“index.html”;
}
} 
这是您的代码,已清理

<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>

函数登录(){
var password=document.getElementById(“密码”).value;
如果(密码==“wima”){//使用==比较值
document.getElementById(“提交”).href=“/pages/home.html”;
}
否则{
document.getElementById(“提交”).href=“index.html”;
}
}
这里的另一个问题是,您不应该更改用于执行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>

函数登录(){
var password=document.getElementById(“密码”).value;
如果(密码==“wima”){
window.location.href=“/pages/home.html”;
}
否则{
window.location.href=“index.html”;
}
}

如果你想根据输入类型文本更改href值,我想你是做错了。你应该对密码输入文本进行模糊/更改事件。根据用户单击href时的密码值,他应该相应地重定向。 看看这个:

函数登录(){
var _password=document.getElementById(“密码”).value;
如果(“wima”==\u密码){
document.getElementById(“提交”).href=“/pages/home.html”;
}否则{
document.getElementById(“提交”).href=“index.html”;
}
}

用户名

密码

这是一个带有开关的表单验证程序

函数validateForm(){
var x=document.forms[“myForm”][“password”].value;
开关(x){
案例“”:
警告(“必须填写姓名”);
返回false;
打破
“wima”案:
返回true;
打破
违约:
警报(“错误:密码错误”);
document.location.href=”https://stackoverflow.com/search?q=notloggedin";
//用错误链接返回替换上面的链接
返回false;
}
}

如果有人检查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>

用户名

密码
提交

var password=document.getElementById('password'); 密码。addEventListener('change',enableLogin); var submit=document.getElementById('submit'); 函数enableLogin(){ 如果(password.value==“wima”){//这是一个很容易犯的错误(=或==) submit.href=“/pages/home.html”; }否则{ submit.removeAttribute('href'); } }
var password=getElementById(“密码”);
应该是
var password=document.getElementById(“密码”).value;
setAttribute('href',“link.html”);谢谢。是否可以在检查时设置我链接的href,然后立即将我带到链接?当我单击“提交”链接时,我希望代码运行并被带到正确的页面。我应该将setAttribute放在哪里?@Nathan忘了这一点,你可以像你一样直接设置
。href
。至于带到正确的页面e、 这确实会在
onclick
函数完成后自动发生,因为它不会阻止标准的“用户单击
”行为。