使用 Javascript 和条件提交表单,必须按两次提交

Submitting form using Javascript with conditions, have to press submit twice

我创建了一个表单,如果有一些特定的条目,我希望表单不应该被提交,并且应该执行一些功能。但是对于所有其他条目,表单应该会被提交并正常工作。

    function checkuser() {
        let name = document.forms[0].name.value;
        let email = document.forms[0].email.value;
        let message = document.forms[0].message.value;
        if ((name == "admin") && (email == "admin@admin.com") && message == ("admin")) {
            alert("hey"); //SOME FUNCTION
        } else {
            document.forms[0].setAttribute("action", "login.php");
            document.forms[0].setAttribute("onsubmit", " ");
            document.forms[0].submit();
        }
      }
    <form method="post" onsubmit="event.preventDefault(); return checkuser(this)" id="form">
    
         <table align="center">
    
             <tr>
                 <th><label for="name"> Name : </label></th>
                 <td> <input type="text" height=100px id="name" name="name" placeholder="Enter your Name..." required> </td>
    
             </tr>
    
             <tr>
                 <th><label for="email"> Email : </label> </th>
                 <td><input type="email" id="email" name="email" placeholder="Enter your Email ID..." required></td>
             </tr>
    
             <tr>
                 <th><label for="phone"> Phone Number : </label> </th>
                 <td><input type="tel" id="phone" name="phone" placeholder="Enter your Phone no..." pattern="[0-9]{10}"></td>
             </tr>
    
             <tr>
                 <th><label for="message"> Message : </label> </th>
                 <td><textarea id="message" name="message" placeholder="Enter Message..." required></textarea></td>
             </tr>
    
         </table>
    
         <div class="buttons">
             <input type="submit" value="Submit" name="submit">
             <input type="reset" value="Reset" name="reset">
         </div>
    </form>

它工作得很好,但问题是,对于正常值,我必须按 SUBMIT 按钮 两次 才能重定向表单到“login.php” 并提交。我认为,当我按一次 时,它删除了“提交”方法并向表单添加了“操作”,然后按了两次,它就被提交了。

我只需要点击一下就可以使用它。如果我输入特殊值,它应该执行下面的函数,对于普通值,它应该直接使用“login.php”作为动作提交表单。

如有任何帮助,我们将不胜感激。

我是这样做的...

    function checkuser() {
        let name = document.forms[0].name.value;
        let email = document.forms[0].email.value;
        let message = document.forms[0].message.value;
        if ((name == "admin") && (email == "admin@admin.com") && message == ("admin")) {
            alert("hey"); //SOME FUNCTION
        } else {
            document.forms[0].submit();
        }
      }
    <form method="post" action="login.php" id="form">
    
         <table align="center">
    
             <tr>
                 <th><label for="name"> Name : </label></th>
                 <td> <input type="text" height=100px id="name" name="name" placeholder="Enter your Name..." required> </td>
    
             </tr>
    
             <tr>
                 <th><label for="email"> Email : </label> </th>
                 <td><input type="email" id="email" name="email" placeholder="Enter your Email ID..." required></td>
             </tr>
    
             <tr>
                 <th><label for="phone"> Phone Number : </label> </th>
                 <td><input type="tel" id="phone" name="phone" placeholder="Enter your Phone no..." pattern="[0-9]{10}"></td>
             </tr>
    
             <tr>
                 <th><label for="message"> Message : </label> </th>
                 <td><textarea id="message" name="message" placeholder="Enter Message..." required></textarea></td>
             </tr>
    
         </table>
    
         <div class="buttons">
             <input type="button" value="Submit" onclick="checkuser();">
             <input type="reset" value="Reset" name="reset">
         </div>
    </form>