【问题标题】:Form Validation not working using onclick and window.open表单验证无法使用 onclick 和 window.open
【发布时间】:2018-04-26 08:39:25
【问题描述】:

我已经尝试了很多来让这个表单工作。早些时候只有“window.open”语句不起作用,但现在“alert”也停止工作,就像函数根本不起作用一样。

<html>

<head>
    <title>
        Validation
    </title>
    <script type="text/javascript" language="javascript">
        function myfunction()
        {
        var a=GetElementById("log");
        var b=GetElementById("pwd");
        if (a=="log" && b=="pwd")
        {
            window.open("https://www.google.com");
        }
        else
        {
            alert("Your username or password is wrong");
        }
        }
    </script>
</head>
<body>
    <form method="post">
        Name: <input type="textbox" id="log" name="log"> <br>
        Password: <input type="password" id="pwd" name="pwd"> <br>
        <input type="submit" value="submit" onsubmit="myfunction();">
    </form>
</body>

【问题讨论】:

    标签: validation onclick window.open


    【解决方案1】:

    到目前为止,您的表单本身已提交,js中也没有GetElementById(),请使用document.getElementById()。尝试以下更改。

    function myfunction() {
      var a = document.getElementById("log").value; /* changed */
      var b = document.getElementById("pwd").value;
      if (a == "abc" && b == "123") {
        window.open("https://www.google.com");
      } else {
        alert("Your username or password is wrong");
      }
      return false;
    }
    <form method="post" onsubmit="return myfunction();">
      <!-- moved -->
      Name: <input type="textbox" id="log" name="log"> <br> Password: <input type="password" id="pwd" name="pwd"> <br>
      <input type="submit" value="submit">
    </form>

    【讨论】:

    • 感谢 Arvind 的帮助,但还是没有用。
    • @AbhishekKataria,你检查错误了吗?按 F12 启动开发人员面板,然后转到控制台选项卡并检查错误(如果有)。
    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多