如何防止 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
How to prevent Jquery append more than once
提问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 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 来检查用户是否输入了正确的电子邮件地址。如果电子邮件无效,用户可以返回并更改他/她的电子邮件。但是,我的代码将导致两次相同的错误消息,因为错误一直附加在我的errors
div 中。如何更改此设置以仅允许附加一次错误?如果email
变量是true
. 此 div 中还存在其他验证错误。
jQuery:
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.
validateEmail
将返回true
或false
取决于电子邮件是否有效。
回答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 email
is true
, add an else
to your if
statement:
并且在email
is时不显示任何内容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 email
to false
and not actually check for equality.
另请注意,我使用了===
而不是=
. 这是因为使用单等号(=
)将只设置变量email
来false
,而不是实际检查是否相等。
回答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 p
tag and add a class="appended"
it works well
你需要做的就是$('.appended').remove()
在你的条件之后添加一个right,然后在你附加的文本中,使用一个p
标签并添加一个class="appended"
它运行良好