Submit a form with jQuery / Javascript without ignoring “required” tag

断了今生、忘了曾经 提交于 2019-12-10 22:02:04

问题


so I have the following form:

    <form action="register.php" method="post" id="register-form" name="register-form">
    <input type="text" id="username" name="username" placeholder="Username" autofocus="autofocus" required />
    <input type="text" id="password" name="password" placeholder="Password" required />
    <a href="javascript:void(0);" class="btn" onclick="$('#register-form').submit();">Register</a>
    <input type="submit" style="display: none;" />
    </form>

What I want to know, is there a way to make jQuery take care of the "required" tag when submitting a form? Right now it doesn't.


回答1:


Trigger click on the submit button instead.

http://jsfiddle.net/PdUcq/




回答2:


From this link,

required attribute works in most new browsers. In older browsers its not supported since its part of the HTML5 spec.

If you are looking for cross browser support with jquery you would have to use some plugin. One of the good ones is this plugin.

Or you can go ahead and write your own custom validators, but why re-invent the wheel.



来源:https://stackoverflow.com/questions/15114594/submit-a-form-with-jquery-javascript-without-ignoring-required-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!