停止jQuery insertAfter多次激发
Stop jQuery insertAfter from firing multiple times
我正在我的web应用程序中进行一些基本验证,检查密码和确认密码字段的值是否匹配。如果没有,我的代码会在两个字段中添加一个类,并在单击提交按钮时显示错误消息。
$("#btn_register").click(function(e){
if( $("#confirmpassword").hasClass('has-error') && $("#password").hasClass('has-error') ){
e.preventDefault();
}
var pass1 = $("#password").val();
var pass2 = $("#confirmpassword").val();
if(pass1 != pass2){
$("#password").addClass('has-error');
$("#confirmpassword").addClass('has-error');
$("<p class='errorMsg'>The Password and confirm password fields do not match </p>").insertAfter("#confirmpassword");
return false;
}
});
问题是,如果多次单击提交按钮,则会多次添加使用insertAfter添加的消息。我在这里检查过类似的问题,并试图让它自己发挥作用,但没有运气。
我错过了什么?感谢
执行
if($("p.errorMsg").length==0)
$("<p class='errorMsg'>The Password and confirm password fields do not match </p>").insertAfter("#confirmpassword");
else
$("p.errorMsg").html(" Any other error if you want to display then set it as HTML ");
这将仅在p
之前未插入的情况下插入。
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用jquery在填充自动完成的值后使文本框只读
- jQuery:循环一个具有不同超时值的循环
- 用程序搜索JQuery数据表中的文本
- JQuery insertAfter();移动下一个表格单元格
- 如何在TextNode之后使用jQuery.insertAfter
- jQuery insertAfter添加了2个相同的元素
- javascript / jQuery “insertAfter” 函数参数中的空格会导致失败
- jQuery .insertAfter() 和 .append() 不适用于 AJAX 调用
- Jquery .insertAfter() 多次插入
- 如何在动态 DOM 元素上使用 .insertAfter 和 jQuery
- 使用带有选择框的 jQuery 的 insertAfter() 是否存在已知的性能问题
- 停止jQuery insertAfter多次激发
- 如何访问使用jquery insertAfter方法创建的元素
- 带有新ID的jQuery.colone.insertAfter
- jQuery中的InsertAfter错误'Uncaught TypeError: Cannot set pro
- jQuery: insertAfter()在Firefox上不起作用
- 将内部数组指针前进到下一条记录以填充jquery函数以使用insertAfter
- jQuery insertAfter:将插入<但不是