我遇到问题我无法删除错误 msg (如果存在);
如果您点击输入然后输出,或者如果无效,则电子邮件显示错误,但再次点击进入并重复出现错误信息,
如果错误存在,我想删除它并再次添加,或者如果存在则不再添加,直到有效?任何想法?
function validEmail(v) {
var r = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return (v.match(r) == null) ? false : true;
}
$(document).ready(function() {
$('input').blur(function() {
var email = $(this).val();
var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
var emailInputId = $(this).attr('id');
if ($("#" + emailInputId + "_error_message").length) {
$("#" + emailInputId + "_error_message").remove();
}
console.log($(emailInputId + "_error_message"));
if (validEmail(email)) {
//alert('valid email');
/*$.ceAjax('request', fn_url('ac.email'), {
method: 'post',
data: {
'email': email
},
caching: true
});*/
$(this).removeClass('cm-failed-field');
$(this).prev().removeClass('cm-failed-label');
$(this).next("span").remove();
} else {
$(this).addClass('cm-failed-field');
$(this).prev().addClass('cm-failed-label');
$(this).after("<span id=" + emailInputId + "_error_message' class='help-inline'><p>" + emailError + "</p></span>");
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input x-autocompletetype="email" type="text" id="elm_32" name="user_data[email]" size="32" value="" class="ty-input-text cm-skip-avail-switch cm-focus cm-failed-field">
&#13;
答案 0 :(得分:1)
更改类(。)的id(#)
html文档中的Id是一个唯一标识元素的属性。
Jquery只在选择器为id(#)时获取第一个元素。
if ($("." + emailInputId + "_error_message").length>0) { $("." + emailInputId + "_error_message").remove(); } $(this).after("<span class=" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
function validEmail(v) {
var r = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return (v.match(r) == null) ? false : true;
}
$(document).ready(function() {
$('input').blur(function() {
var email = $(this).val();
var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
var emailInputId = $(this).attr('id');
if ($("." + emailInputId + "_error_message").length>0) {
$("." + emailInputId + "_error_message").remove();
}
if (validEmail(email)) {
//alert('valid email');
/*$.ceAjax('request', fn_url('ac.email'), {
method: 'post',
data: {
'email': email
},
caching: true
});*/
$(this).removeClass('cm-failed-field');
$(this).prev().removeClass('cm-failed-label');
$(this).next("span").remove();
} else {
$(this).addClass('cm-failed-field');
$(this).prev().addClass('cm-failed-label');
$(this).after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input x-autocompletetype="email" type="text" id="elm_32" name="user_data[email]" size="32" value="" class="ty-input-text cm-skip-avail-switch cm-focus cm-failed-field">
&#13;