如何防止Jquery追加多次

发布于 2024-12-24 19:51:53 字数 509 浏览 2 评论 0原文

我有一些 jQuery 来检查用户是否输入了正确的电子邮件地址。如果电子邮件无效,用户可以返回并更改他/她的电子邮件。但是,我的代码将导致相同的错误消息两次,因为错误不断附加到我的 errors div 中。如何更改此设置以仅允许附加错误一次?如果 email 变量为 true,我还想删除该错误。此 div 中还存在其他验证错误。

jQuery:

var email = $('#email').val();
email = validateEmail(email);
if (email = "false") {
    $('#errors').append("<p>Please enter a valid email</p>");
}

validateEmail 将返回 truefalse,具体取决于电子邮件是否有效。

I have some jQuery that checks if the user inputted a correct email address. The user can then go back and change his/her email if it is not valid. However, my code will result in the same error messege twice because the error keeps getting appended inside my errors div. How can I change this to only allow the error to be appended once? I also would like to remove the error if the email variable is true. There are also other validation errors being placed in this div.

jQuery:

var email = $('#email').val();
email = validateEmail(email);
if (email = "false") {
    $('#errors').append("<p>Please enter a valid email</p>");
}

validateEmail will return either true or false depending on whether the email is valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

给妤﹃绝世温柔 2024-12-31 19:51:53
var email = $('#email').val();
email = validateEmail(email);
if (email == "false"){
      $('#email_error').remove();
      $('#errors').append("<p id='email_error'>Please enter a valid email</p>");
}
var email = $('#email').val();
email = validateEmail(email);
if (email == "false"){
      $('#email_error').remove();
      $('#errors').append("<p id='email_error'>Please enter a valid email</p>");
}
感情旳空白 2024-12-31 19:51:53

当您附加电子邮件错误时,请使用类“emailerror”指定它,

$('#errors').append("<p class="emailerror">Please enter a valid email</p>");

但在附加之前,只需删除具有类“emailerror”的错误

var email = $('#email').val();
email = validateEmail(email);
if (email = "false"){
    $('#errors .emailerror').remove();
    $('#errors').append("<p class="emailerror">Please enter a valid email</p>");
}

when u append the email error, specity it with a class "emailerror",

$('#errors').append("<p class="emailerror">Please enter a valid email</p>");

but before append, just remove that error which have class "emailerror"

var email = $('#email').val();
email = validateEmail(email);
if (email = "false"){
    $('#errors .emailerror').remove();
    $('#errors').append("<p class="emailerror">Please enter a valid email</p>");
}
谎言月老 2024-12-31 19:51:53

尝试在附加新信息(即新错误)之前清除该值:

$('#errors').val('');

Try clearing the value before appending new information, ie new errors:

$('#errors').val('');
看海 2024-12-31 19:51:53

使用html()text()设置错误的内容div

$('#errors').html("<p>Please enter a valid email</p>");

并且在email 为 true,请在 if 语句中添加 else

if (email === "false"){
    $("#errors").html("<p>Please enter a valid email</p>");
} else {
    $("#errors").html("");
}

另请注意,我使用了 === =。这是因为使用单个等号 (=) 只会将变量 email 设置为 false 而不会实际检查是否相等。

Use html() or text() to set the content of the errors div:

$('#errors').html("<p>Please enter a valid email</p>");

And to not display anything when email is true, add an else to your if statement:

if (email === "false"){
    $("#errors").html("<p>Please enter a valid email</p>");
} else {
    $("#errors").html("");
}

Also note that I used === instead of =. This is because using a single equals sign (=) will just set the variable email to false and not actually check for equality.

时光礼记 2024-12-31 19:51:53

更合适的方法是从 DOM 树中删除所有附加代码:

试试这个

var email = $('#email').val();
email = validateEmail(email);
if (email == "false"){
      $('#email_error').parent().remove();
      $('#errors').append("<p id='email_error'>Please enter a valid email</p>");
}

More appropriate way is to remove all the appended code from DOM tree:

Try this:

var email = $('#email').val();
email = validateEmail(email);
if (email == "false"){
      $('#email_error').parent().remove();
      $('#errors').append("<p id='email_error'>Please enter a valid email</p>");
}
黯然#的苍凉 2024-12-31 19:51:53

您需要做的就是在条件之后添加 $('.append').remove() ,然后在要附加的文本中使用 p 标记并添加一个 class="append" 它效果很好

all you need to do is add a $('.appended').remove() right after your conditional, then in the text you are appending, use a p tag and add a class="appended" it works well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文