使用jQuery替换“谢谢你”的形式? [英] Use jQuery to replace form with 'Thank You'?
问题描述
你好可以在提交后让联系表单隐藏吗?
有消息说谢谢表单是在哪里?
< ; form id =form>
< input type =textname =nameplaceholder =Your name>
< input type =submitvalue =发送>
< / form>
< div id =thanksstyle =display:none;>谢谢!< / div>
var $ form = $('#form');
$ b $ form.submit(function(evt){
evt.preventDefault();
$ .ajax(function(){
url:send.php ,
type:POST,
data:$ form.serialize()
})。done(function(phpSays){
if(phpSays ==OK ){//让你的.php脚本返回一个OK字符串
$('#thanks')。show();
} else {
//如果发生了什么,错误
}
});
});
您可以注意到< form>
不具有动作和方法属性,
会导致所有将通过对您的.php脚本的AJAX调用进行页面刷新处理。
此示例可能因此失败JS被禁用的可怜家伙(可以用< noscript>
info元素进行操作),但对防止加载垃圾邮件很有用(通常垃圾机器人会在没有JS支持的情况下搜索上面提到的方法和 action 属性)。 com / jquery.ajax /rel =nofollow> http://api.jquery.com/jquery.ajax/
Hi is it possible to make a contact form hide after hitting submit? With a message saying Thank you where the form was?
<form id="form">
<input type="text" name="name" placeholder="Your name">
<textarea name="message" placeholder="Message"></textarea>
<input type="submit" value="Send">
</form>
<div id="thanks" style="display:none;">Thank you!</div>
var $form = $('#form');
$form.submit(function( evt ){
evt.preventDefault();
$.ajax(function(){
url : "send.php",
type : "POST",
data : $form.serialize()
}).done(function( phpSays ){
if( phpSays == "OK" ){ // make your .php script return an "OK" string
$('#thanks').show();
}else{
// DO something else if something went wrong
}
});
});
You can notice that the <form>
doesn't have the action and method attributes,
cause all that will be handled without page refresh by an AJAX call to your .php script.
This example might fail for that poor guy with JS disabled (handlable with a <noscript>
info element) but useful to prevent load of junk emails (cause usually junk bots will crawl without JS support searching for the above mentioned method and action attributes).
http://api.jquery.com/jquery.ajax/
这篇关于使用jQuery替换“谢谢你”的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!