简体   繁体   English

"使用 javascript 提交表单并需要输入"

[英]Form submit with javascript and input required

I was trying to submit things using a div, HTML5, JavaScript.我试图使用 div、HTML5、JavaScript 提交东西。 If I use a submit button the validator (required attribute in the inputs) works the button doesn't submit info.如果我使用提交按钮,则验证器(输入中的必需属性)工作该按钮不会提交信息。 But if I use a div tag it doesn't validate and send the info.但如果我使用 div 标签,它不会验证和发送信息。 Any way to fix it?有什么办法解决吗? Or should I do the whole code to validate the info and stop the submit?还是我应该执行整个代码来验证信息并停止提交?

<form action="index.php" method="get" name='myform'>
  <ul>
    <li class="row">
      <label class="labels" for="username">Username</label>
      <input type="text" id="txtUser" required>
    </li>
    <li class="row">
      <label class="labels" for="password">Password</label>
      <input type="password" id="txtPass" required>
    </li>
    <li id="row2">
      <div class="button"><a href="javascript: submitform()">Submit</a></div>
      <input type="submit">
    </li>
  </ul>
</form>

If you are submitting via JS then you need to call the form validation yourself.如果您通过 JS 提交,那么您需要自己调用表单验证。 I don't see any issues in your post that would prevent the submit though (without validation).我在您的帖子中没有看到任何会阻止提交的问题(未经验证)。 But here's a working example.但这是一个工作示例。 Keep in mind that not all browsers support html5 validation.请记住,并非所有浏览器都支持 html5 验证。

 function submitform() { // Get first form element var $form = $('form')[0]; // Check if valid using HTML5 checkValidity() builtin function if ($form.checkValidity()) { console.log('valid'); $form.submit(); } else { console.log('not valid'); } return false }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="index.php" method="get" name='myform'> <ul> <li class="row"> <label class="labels" for="username">Username</label> <input type="text" id="txtUser" name="sds" required> </li> <li class="row"> <label class="labels" for="password">Password</label> <input type="password" id="txtPass" required> </li> <li id="row2"> <div class="button"><a href="" onclick="return submitform()">Submit</a></div> <input type="submit"> </li> </ul> </form>

I had a similiar problem and solved it this way.我有一个类似的问题并以这种方式解决了。

You can use a function to not handle it on element.您可以使用函数不在元素上处理它。

"

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

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