简体   繁体   English

在班级变更时显示警报

[英]Show alert on class change

I've been busy for some time now and can't seem to get this one. 我已经忙了一段时间了,似乎无法得到这个。 It should be really simple (I guess). 它应该非常简单(我想)。

I have a form on my site with ID #form-custom . 我的网站上有一个ID #form-custom When you click on the submit button and some fields are not correct, it will display an error message and a class will be added to the form, class .error . 当您单击提交按钮并且某些字段不正确时,它将显示一条错误消息,并且一个类将添加到表单class .error

What I am trying to do is add an eventlistener or something else that displays an alert when the class of #form-custom changes to id="form-custom" class="error" . 我想做的是添加一个事件侦听器或其他东西,当#form-custom的类更改为id="form-custom" class="error"时,它会显示警报。

So far I have tried these codes: 到目前为止,我已经尝试了以下代码:

document.getElementById("form-custom").addEventListener("click", classchange);
    function classchange() {
        if document.hasClass.("error");
        alert("boe");
    }

and

if ($("#form-custom").hasClass('error')) {
            alert('boe.');
        });

and

$(document).ready(function(){

    $('form#form-custom').click(function(){    
        if ($(this).hasClass('error')) {
            alert('boe.');
        }
    });
}

And some variations of these codes. 以及这些代码的一些变体。 But non do what I would like them to do... 但是不做我希望他们做的事情...

Could some one help me out? 有人可以帮我吗?

Form submit should be the event that should work for your situation 表单提交应该是适合您情况的事件

$('#form-custom').on("submit", function()
{
     if($(this).hasClass("error"))
     {
         alert("Validation fail message");
         return false;
     }

     // submit form

});

You could use a MutationObserver 您可以使用MutationObserver
I would use something like this: 我会用这样的东西:

$("#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM