gpt4 book ai didi

javascript - 验证 : remove custom success message when error occurs

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

我正在使用 jQuery 验证来使用远程调用来检查用户名是否存在。像这样:

remote: {
url: "MyUrl",
type: "GET",
complete: function (data) {
// Return success message if applicable
if (data.responseText == "true")
{
$("#divUserNameAvailable").show();
}
}
}

但是,如果我留在现场并强制验证错误,该消息就会保留。

enter image description here

这是完整的验证:

if ($("#CreateAccountForm").length) {
$("#CreateAccountForm").validate({
rules: {
UserName: {
required: true,
rangelength: [6, 20],
usernameFilter: true,
remote: {
url: "MyUrl",
type: "GET",
complete: function (data) {
// Return success message if applicable
if (data.responseText == "true")
{
$("#divUserNameAvailable").show();
}
}
}
},
},
messages: {

UserName: {
remote: "This username is not available. Please enter a new username.",
required: "Please enter your username.",
rangelength: "Usernames must be between 6 and 20 characters in length and may not use any special characters such as \\ / + {{ }} [ ] | = , * ? ; : < > @."
},

},

errorPlacement: function (error, element) {
error.insertAfter(element);
},
submitHandler: function (form) {
form.submit();
}
});

我尝试将 DIV 隐藏在 errorPlacement 中,但没有任何作用。有什么想法吗?

最佳答案

如果调用成功,则隐藏错误<div> :

remote: {
url: "MyUrl",
type: "GET",
complete: function (data) {
// Return success message if applicable
if (data.responseText == "true")
{
$("#divUserNameAvailable").show();
// Hide the error div. Replace the `that_error_div_selector` with right selector.
$(that_error_div_selector).hide();
}
}
}

关于javascript - 验证 : remove custom success message when error occurs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38230497/

25 4 0