简体   繁体   English

使用带有条件的 Javascript 提交表单,必须按两次提交

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

I have created a form in which I want that if there are some specific entries, the form should not get submitted, and some functions should be executed.我创建了一个表单,如果有一些特定的条目,则不应提交该表单,并且应该执行某些功能。 But for every other entry, the form should get submitted and work as normal.但是对于每个其他条目,表单应该被提交并正常工作。

 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>

It works just fine, but the problem is just that, for normal values, I have to press SUBMIT button twice to redirect the form to " login.php" and submit it.它工作得很好,但问题在于,对于正常值,我必须按两次提交按钮才能将表单重定向到“ login.php”并提交。 I think, when I press it once, it removes the "submit" method and adds the "action" to the form, and on pressing it twice, it is submitted.我认为,当我按下它一次时,它会删除“提交”方法并将“操作”添加到表单中,然后按下两次,它就会被提交。

I need it to work with just one click .我需要它,只需点击一下工作。 If I input special values, it should execute the following function, and for normal values, it should directly submit the form using "login.php" as an action.如果我输入特殊值,它应该执行以下函数,对于正常值,它应该使用“login.php”作为动作直接提交表单。

Any help would be appreciated.任何帮助,将不胜感激。

I did it like this...我是这样做的...

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM