我正在做一个文本框,如果不满足要求,那么背景将变为黄色,但我只想背景保持黄色 3 秒......我该怎么做?
这就是我现在所拥有的,但黄色背景仍然存在......
$("#form").on("submit",function(){
user = $("#username").val().length;
userErrorBg = $('#username').css('background','yellow');
if(user < 3 || user > 6)
{
setInterval("userErrorBg",0);
}
})
我正在考虑在某处放置一个clearInterval,但我需要一个时间计数,对吗?
最佳答案
如果您认为 setTimeout(function(){},time) 在特定时间段内仅工作一次,则最好使用 setTimeout(function(){},time),而不是 setInterval(function(){}, time) )
在给定的特定时间段后重复该函数。
试试这个:
$("#form").on("submit", function () {
user = $("#username").val().length;
if (user < 3 || user > 6) {
$('#username').css('background', 'yellow'); //<---need to put it here
setTimeout(function () {
$('#username').css('background', 'white').focus(); //<--add focus too
}, 3000);
return false;
}
});
CHECKOUT THE DEMO FIDDLE
This POST could help you .
关于jquery - 如何使用jquery改变文本框的背景颜色3秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15674762/