[英]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.