最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Form submit with javascript and input required - Stack Overflow

programmeradmin3浏览0评论

I was trying to submit things using a 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. 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>

Im using the following code. Javascript submit: .phtml

I was trying to submit things using a 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. 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>

http://www.dropmocks./mCyfGJ

Im using the following code. Javascript submit: http://www.javascript-coder./javascript-form/javascript-form-submit.phtml

Share Improve this question edited May 3, 2023 at 12:01 jsadev 2,5901 gold badge18 silver badges29 bronze badges asked Jan 13, 2014 at 1:41 nanoalvareznanoalvarez 932 gold badges4 silver badges10 bronze badges 2
  • 1 show me your code, so i can help you. – F.C.Hsiao Commented Jan 13, 2014 at 1:44
  • i guess this is what you looking for stackoverflow./questions/8053394/… – F.C.Hsiao Commented Jan 13, 2014 at 1:46
Add a ment  | 

2 Answers 2

Reset to default 8

If you are submitting via JS then you need to call the form validation yourself. 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.

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

<input type="submit" id="Guardar" name="Guardar" value="Guardar" onClick="if(this.form.checkValidity()){this.form.submit(); this.disabled=true; this.value='Enviando…';}else{this.form.checkValidity();}">

You can use a function to not handle it on element.

发布评论

评论列表(0)

  1. 暂无评论