2017-10-21 136 views
0

我正在尝试使用户单击Submit按钮时,它们被重定向到另一个页面。另外,如果他们尝试提交而没有输入他们的名字,姓氏或电子邮件,它将返回一个错误,指出“这是必填字段”。出于某种原因,这两者都不起作用。单击提交时没有任何反应。我相信这是简单的,但我从来没有使用过这些脚本,所以我不知道我在找什么。JavaScript中的必需字段和重定向

下面是代码:

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

任何帮助将不胜感激!

回答

0

试试这个:

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

这做到了!我不得不添加https://以使其链接到实际的页面,但除此之外它完美地工作! – Noah210012

0

快速的方式,因为它看起来像你不使用ajax。

从提交按钮中删除onclick(因此需要标记被忽略)以及JavaScript,并且如果成功,则返回来自服务器的URL重定向302。