[英]Show alert on class change
我已经忙了一段时间了,似乎无法得到这个。 它应该非常简单(我想)。
我的网站上有一个ID #form-custom
。 当您单击提交按钮并且某些字段不正确时,它将显示一条错误消息,并且一个类将添加到表单class .error
。
我想做的是添加一个事件侦听器或其他东西,当#form-custom
的类更改为id="form-custom" class="error"
时,它会显示警报。
到目前为止,我已经尝试了以下代码:
document.getElementById("form-custom").addEventListener("click", classchange);
function classchange() {
if document.hasClass.("error");
alert("boe");
}
和
if ($("#form-custom").hasClass('error')) {
alert('boe.');
});
和
$(document).ready(function(){
$('form#form-custom').click(function(){
if ($(this).hasClass('error')) {
alert('boe.');
}
});
}
以及这些代码的一些变体。 但是不做我希望他们做的事情...
有人可以帮我吗?
表单提交应该是适合您情况的事件
$('#form-custom').on("submit", function()
{
if($(this).hasClass("error"))
{
alert("Validation fail message");
return false;
}
// submit form
});
您可以使用MutationObserver
我会用这样的东西:
$("#form-custom").on("submit",function(){
var me = $(this);
// wait for the other onSubmit to set class
setTimeout(function(){
if(me.hasClass("error")) {
alert("error")
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.