html5 required and jQuery submit()

后端 未结 9 595
我在风中等你
我在风中等你 2020-12-09 07:56

I\'m trying to implement html5 into a form, but I came with a problem when I submit the form through jquery and try to use the html5 \"required\" attribute.

Here is

相关标签:
9条回答
  • 2020-12-09 08:57

    You can

    • trigger click event on a submit
    • check validation manually with $("form")[0].checkValidity()
    • find invalid elements manually using $("form :invalid")
    0 讨论(0)
  • 2020-12-09 09:01

    Here are some examples, how to validate html5:

    function validateForm(form) {
      if (form.username.value=='') {
        alert('Username missing');
        return false;
      }
      return true;
    }
    
    <form action="index.php" name="loginform" method="post" onsubmit="if (!validateForm(this)) event.preventDefault();">
      <input name="username" placeholder="username" required="required" type="text" />
      <a href="" onclick="event.preventDefault(); $(document.forms.loginform).submit();">Login1</a>
      <a style="cursor:pointer;" onclick="$(document.forms.loginform).submit();">Login2</a>
      <input name="send" type="submit" value="Login3" />
      <input name="send" type="button" value="Login4" onclick="$(document.forms.loginform).submit();" />
    </form>
    

    Full code here. One thing here is strange: $(document.forms.loginform).submit(); works fine, but document.forms.loginform.submit(); fails. Can somebody explain why?

    0 讨论(0)
  • 2020-12-09 09:01

    Working demo http://jsfiddle.net/3gFmC/

    Hope this will help :)

    code

    $('#btn_submit').bind('click', function() {
    
            $('input:text[required]').parent().show();
            // do whatever;
     });
    

    0 讨论(0)
提交回复
热议问题