【发布时间】:2019-02-01 08:09:55
【问题描述】:
我在表单中的输入元素上使用了 html5 的 required 属性。通常,当您使用它们并提交表单时,您会收到一条消息,哪些元素无效。
我处于一种特殊情况,需要我在发布之前测试输入字段的状态。 (如果一切正常,那么其他人的代码(Wordpress 中的另一个插件)会通过模拟点击另一个提交元素来触发。)
这是相关 jQuery 代码的 sn-p:
var t = $(this);
found_required = false;
$("input").each(function() {
if ( $(this).hasClass('required') ) {
if ($(this).is(":invalid")) {
t.removeClass('swishmode');
//Tell user this element is required
found_required = true;
}
}
});
if ( found_required == true ) {
$('form.give-form').attr('action','Javascript:void();'); //Just an attempt
$(this).click(); //Just an attempt
return; //Returns user back to form without doing different things below
}
//Code to do different things...
我的问题是 - 有没有办法显示默认错误,就像提交带有无效数据的表单时显示的那样?(或者您是否必须为此编写自定义错误处理)
实际代码就像我现在所拥有的那样工作,但用户没有得到指示哪些字段不正确。我知道我可以只为这些输入元素添加一些样式,如果上述方法不可行,我会这样做。
【问题讨论】:
标签: jquery html input message required