【问题标题】:Required Fields and Redirects in JavaScriptJavaScript 中的必填字段和重定向
【发布时间】:2017-10-21 22:10:35
【问题描述】:

我正在尝试这样做,以便当用户单击提交按钮时,他们会被重定向到另一个页面。此外,如果他们尝试在不输入名字、姓氏或电子邮件的情况下提交,则会返回错误消息“这是必填字段”。出于某种原因,这两种方法都不起作用。单击提交时没有任何反应。我确信这很简单,但我以前从未使用过这些脚本,所以我不知道我在寻找什么。

代码如下:

<form method="post" enctype="multipart/form-data">
            <button><input type="submit" value="Submit" onclick="submit();"/></button><br>
            <label for="fname">First Name:</label>
            <input type="text" required/><br>
            <label for="lname">Last Name:</label>
            <input class="lname" type="text" required/><br>
            <label for="email">Email:</label>
            <input class="email" type="text" required/><br>
            <input type="radio" name="file" value="yes" id="yes" />
            <label for="Yes">Yes</label>
            <input type="radio" name="file" value="no" id="no" />
            <label for="No">No</label><br>
            <p><input type="file" size="30" required></p>
</form>

        <script>
            function submit() {
                window.location= "http://stackoverflow.com"
            }
        </script>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript html forms redirect webforms


    【解决方案1】:

    试试这个:

    <form method="post" enctype="multipart/form-data" onsubmit="return validateForm()">
            <input type="submit" value="Submit" /><br>
            <label for="fname">First Name:</label>
            <input type="text" required /><br>
            <label for="lname">Last Name:</label>
            <input class="lname" type="text" required/><br>
            <label for="email">Email:</label>
            <input class="email" type="text" required/><br>
            <input type="radio" name="file" value="yes" id="yes" />
            <label for="Yes">Yes</label>
            <input type="radio" name="file" value="no" id="no" />
            <label for="No">No</label><br>
            <p><input type="file" size="30" required></p>
    </form>
    <script>
        function validateForm() {
          window.open("https://www.stackoverflow.com");
        }
    </script>
    

    【讨论】:

    • 做到了!我必须添加 https:// 以使其链接到实际页面,但除此之外它可以完美运行!
    【解决方案2】:

    看起来你没有使用 ajax 的快速方法。

    从提交按钮中删除 onclick(因此忽略了 require 标记)以及 JavaScript,如果成功,则从服务器返回带有 url 的 302 重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 2021-12-10
      • 2021-12-13
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多