gpt4 book ai didi

javascript - JQuery 验证 ResetForm 不起作用

转载 作者:行者123 更新时间:2023-12-03 06:00:52 25 4
gpt4 key购买 nike

我有一个非常简单的问题:有一个带有清除表单按钮的表单。当我点击它时,所有字段都会重置。但我还有一个额外的验证规则:至少应填写一个附加字段。当然,清除后它们都是空的,但我不想看到这些验证消息。我希望它清除整个表单,隐藏所有验证消息等等。这是我的代码:

$("a[data-clear]").click(function (event) {
var now = new Date();
$("#report_search section:gt(0) input").val("");
$("#includeDerived").prop("checked", true);
$("#includeApp").prop("checked", true);
$("#includeOrg").prop("checked", false);
$("input[name='FromDate']").datepicker().datepicker("setDate", now.dateAdd("year", -1));
$("input[name='ToDate']").datepicker().datepicker("setDate", now);
$("form").validate().resetForm();
event.preventDefault();
});

我的页面上只有一个表单,因此多个表单不是问题。

期望的结果:表单被清除,验证消息不显示。

实际结果:表单被清除,验证消息仍然存在。

<小时/>

检查字段是否已填写的示例规则:

jQuery.validator.addMethod("isSufficientInfo",
function (value, element, params) {
var hasPersonInfo = $("input[name='LastName']").val() && $("input[name='FirstName']").val();
if (hasPersonInfo) {
return true;
}
var hasDocInfo = $("select[name='D']").val() && $("input[name='C']").val() && $("input[name='E']").val();
if (hasDocInfo) {
return true;
}
return $("input[name='A']").val() || $("input[name='B']").val();
}, "File some fields");
$("#IsEnoughInfo").rules("add", { isSufficientInfo: "" });

最佳答案

如果您仍在寻找答案,我怀疑 $("form").validate() 创建了一个新的验证器实例。您需要的是之前创建的实例:

$("form").data("validator").resetForm()

或者将验证器存储在变量中:

var v = $("form").validate()
v.resetForm()

关于javascript - JQuery 验证 ResetForm 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39754144/

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