Javascript 10 秒后淡出消息,使用 jQuery 清除输入元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3488526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fade out message after 10 seconds, clearing the input elements using jQuery
提问by input
I'm displaying a message (#final_msg) after the user submits the form. What I want to do is that, after 15 seconds, the message (#final_msg) should fade out, clearing away [or fading out] the text in the input elements as well. Is it possible to do this?
我在用户提交表单后显示一条消息 (#final_msg)。我想要做的是,在 15 秒后,消息 (#final_msg) 应该淡出,清除 [或淡出] 输入元素中的文本。是否有可能做到这一点?
else {
//create post data
var postData = {
"name" : $("#name").val(),
"email" : $("#email").val(),
"msg" : $("#msg").val(),
"origin" : $("#origin").val()
};
//make the call
$.ajax({
type: "POST",
url: "test.php",
data: postData, //send it along with your call
success: function(response){
$('#final_msg').fadeIn();
}
});
}
回答by Benjamin Wohlwend
回答by meder omuraliev
setTimeout(function() {
$('#final_msg').fadeOut();
$('#name, #email, #msg, #origin').val('')
}, 10000 );
回答by Wolfram
You should look into the JavaScript setTimeout() method. With it, you may execute some function after a specific number of milliseconds.
您应该查看JavaScript setTimeout() 方法。有了它,您可以在特定的毫秒数后执行某些功能。
setTimeout(function(){$('#final_msg').fadeOut();}, 10000);
Besides the fading, you may clear all inputs in that function, e.g. by using $(':input').val("") and $(':input').removeAttr("checked") for radio buttons and checkboxes respectively (if there are any that is).
除了淡入淡出之外,您还可以清除该函数中的所有输入,例如分别对单选按钮和复选框使用 $(':input').val("") 和 $(':input').removeAttr("checked") (如果有的话)。

