gpt4 book ai didi

jquery验证: how to prevent on blur (focus out) validation if focus is lost to a submit button

转载 作者:行者123 更新时间:2023-12-03 23:05:33 24 4
gpt4 key购买 nike

我有一个文本框。如果在文本框中输入无效值后单击提交按钮,我不希望对模糊进行验证。

如果我模糊到任何其他控件 - 那么我确实希望进行验证。

对于这种精确情况,Windows [桌面] 表单允许将控件指定为“no-causing-validation”。

是否可以在 jquery validate 中将控件指定为“no-cause-validation”?

最佳答案

是的,这是可能的,但是您必须以代码而不是声明的方式来完成。

为此,您必须覆盖事件的“on”处理程序 - 在本例中为模糊(焦点输出)

here is an example on jsfiddle ,验证这样的代码

$("form").validate({
onfocusout: function (element, event) {
if (element.id === "fname") {
this.element(element);
}
}
});

像这样的html

<form>
<label for="fname">First Name</label>
<input name="fname" id="fname" class="required number">
<label for="sname">Last Name</label>
<input name="sname" id="sname" class="required number">
<input type="submit" />
</form>

这是一个基本示例,验证一个字段是否模糊,而不验证另一个字段。您可以通过检查要验证的字段的属性来进一步采取这一点,但一般方法是

  1. 使用函数覆盖 onfocousout(或 onkeyup 等)
  2. 在该函数中,对于要验证的字段,请调用 element(...)

在此讨论 - https://github.com/jzaefferer/jquery-validation/issues/564

关于jquery验证: how to prevent on blur (focus out) validation if focus is lost to a submit button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17089676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com