如何防止 Jquery 追加不止一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8737608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:58:21  来源:igfitidea点击:

How to prevent Jquery append more than once

jqueryappendemail-validation

提问by kirby

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 errorsdiv. How can I change this to only allow the error to be appended once? I also would like to remove the error if the emailvariable is true. There are also other validation errors being placed in this div.

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

jQuery:

jQuery:

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

validateEmailwill return either trueor falsedepending on whether the email is valid.

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

回答by redmoon7777

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>");
}

回答by Bhavesh Gangani

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

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

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

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

但在追加之前,只需删除具有“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>");
}

回答by Shree

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

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

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

回答by Purag

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

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

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

And to not display anything when emailis true, add an elseto your ifstatement:

并且在emailis时不显示任何内容true,请else在您的if语句中添加一个:

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 emailto falseand not actually check for equality.

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

回答by Kushal Jayswal

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

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

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>");
}

回答by Olagoke Mills

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

你需要做的就是$('.appended').remove()在你的条件之后添加一个right,然后在你附加的文本中,使用一个p标签并添加一个class="appended"它运行良好