使用Javascript提交HTML5表单并验证其输入 [英] Submit HTML5 Form using Javascript and validate its inputs

查看:114
本文介绍了使用Javascript提交HTML5表单并验证其输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写表单并在其输入中添加html5验证属性,如required,autofocus。我使用Javascript来使用document.myForm.submit()提交表单,但它不会根据html5验证属性验证表单,因为它们不存在。

I'm writing form and adding html5 validation attributes to its input like "required", "autofocus". I use Javascript to submit the form using document.myForm.submit() but it doesn't validate the form against the html5 validation attributes as they aren't there.

任何建议?

推荐答案

似乎在提交时触发点击按钮确实有正确的效果: http://jsfiddle.net/e6Hf7/

It appears that triggering a click on the submit button does have the correct effect: http://jsfiddle.net/e6Hf7/.

document.myForm.submitButton.click();

如果您没有提交按钮,请添加一个不可见的按钮,如:

and in case you don't have a submit button, add an invisible one like:

<input type="submit" style="display:none" name="submitButton">

这篇关于使用Javascript提交HTML5表单并验证其输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆