admin管理员组文章数量:1323349
What I am doing is catching the click action on my form's submit button, preventDefault()
ing it, doing some putation, and then making an ajax call instead of submitting the form.
Is there a way to use javascript to force the html5 validators to fire even if the form isn't being submitted?
$('#submitButton').click(function (e) {
e.preventDefault(); //stop the form from submitting
//Do putation
$.post('/ments', values, function(results) {
hideForm($('#new_ment_' + parentId));
parent.children('.children').prepend(results);
}, 'html');
});
What I am doing is catching the click action on my form's submit button, preventDefault()
ing it, doing some putation, and then making an ajax call instead of submitting the form.
Is there a way to use javascript to force the html5 validators to fire even if the form isn't being submitted?
$('#submitButton').click(function (e) {
e.preventDefault(); //stop the form from submitting
//Do putation
$.post('/ments', values, function(results) {
hideForm($('#new_ment_' + parentId));
parent.children('.children').prepend(results);
}, 'html');
});
Share
Improve this question
asked Feb 14, 2012 at 5:53
Razor StormRazor Storm
12.3k20 gold badges95 silver badges151 bronze badges
1
- Yes I can roll my own validation in javascript, but html5's required="required" tag is nice and simple and sets up a decently looking popup GUI for you. Less work and less code plexity is always a plus, but it ends up taking too much work to use html5's validations I can just make my own javascript ones. – Razor Storm Commented Feb 14, 2012 at 5:53
2 Answers
Reset to default 8It works just fine, I tested in latest Chrome and Firefox. Just e.preventDefault()
on the <form>
:
html:
<form method="post" action="test.html">
<input type="text" required></input>
<input type="submit" value="Submit">
</form>
jQ:
$('form').submit(function(e){ e.preventDefault(); });
example: http://jsfiddle/elclanrs/2nnLc/2/
Using html5 constraints the form object has some new methods. For example you can call checkValidity() on the form object to check the input.
<form method="post" action="test.html" id="myForm">
<input type="text" required></input>
<input type="submit" value="Submit" id="submitButton">
</form>
<script type="text/javascript">
$('#submitButton').click(function (e) {
e.preventDefault();
alert($("#myForm").get()[0].checkValidity());
})
</script>
本文标签: javascriptUse html5 validations even if form is never being submittedStack Overflow
版权声明:本文标题:javascript - Use html5 validations even if form is never being submitted? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742133365a2422262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论