我正在做一些jQuery表单验证,但我遇到了一个问题。到目前为止,我有以下代码:
// catch any form submission
$('form').submit(function () {
'use strict';
// if the browser doesn't support HTML5's required attribute
if (!Modernizr.input.required) {
// catch any field that should be required
$(this).find('input[required]').each(function () {
// if is empty
if ($(this).val() === '') {
// create a span that contains a warning to the user
var requiredFieldWarning = document.createElement('span');
requiredFieldWarning.text = 'This field is required.';
// display the span next to the current field
}
});
}
});
我正在尝试在span
未验证的提交表单的任何输入旁边“附加”或显示一个,但我不知道如何。我想不显眼地做到这一点,这就是我span
在 JavaScript 中创建上述内容的原因。
另外,如果提交的表单的任何字段未验证,我如何防止表单被提交?