Javascript 发送消息后清除表单输入
发送表单后,我正在尝试清除表单消息输入字段。虽然它在有机会发送数据之前就清除了数据。 这是我的密码:Javascript 发送消息后清除表单输入,javascript,jquery,ajax,Javascript,Jquery,Ajax,发送表单后,我正在尝试清除表单消息输入字段。虽然它在有机会发送数据之前就清除了数据。 这是我的密码: $(函数(){ $('form#SendForm')。关于('submit',函数(e){ $.post('sendmessagefriend.php',$(this).serialize(),函数(数据){ //这是在成功调用mail.php时执行的。 //“数据”包含来自请求的响应 }).错误(函数(){ //这是在调用mail.php失败时执行的。 }); e、 预防默认值(); });
$(函数(){
$('form#SendForm')。关于('submit',函数(e){
$.post('sendmessagefriend.php',$(this).serialize(),函数(数据){
//这是在成功调用mail.php时执行的。
//“数据”包含来自请求的响应
}).错误(函数(){
//这是在调用mail.php失败时执行的。
});
e、 预防默认值();
});
});
$('#SendForm')。在('click',函数(){
$('#SendForm').find('input:text').val('');
$('input:checkbox')。removeAttr('checked');
});
表格:
不是单击处理程序,而是将
$('#SendForm').find('input:text').val('');
$('input:checkbox').removeAttr('checked');
在成功的回调中
$('form#SendForm').on('submit', function(e) {
$.post('sendmessagefriend.php', $(this).serialize(), function (data) {
// This is executed when the call to mail.php was succesful.
// 'data' contains the response from the request
$('#message').val('');
})
.error(function() {
// This is executed when the call to mail.php failed.
});
e.preventDefault();
});
如果您不关心请求是否成功,只想清除输入,请将其放在ajax调用之后,如
$('form#SendForm').on('submit', function(e) {
$.post('sendmessagefriend.php', $(this).serialize(), function (data) {
// This is executed when the call to mail.php was succesful.
// 'data' contains the response from the request
})
.error(function() {
// This is executed when the call to mail.php failed.
});
$('#message').val('');
e.preventDefault();
});
它起作用了!虽然它清除了我的所有其他领域,除了message@CasperRound,message元素的HTML是什么样子的?我会将它添加到我的帖子1中sec@CasperRound尝试now@Caspar这是一个很好的建议:尽量避免在帖子中出现水平滚动条。水平滚动条会使阅读文本感到不舒服。如果出现.error,则可以处理错误。如果可以放置清空输入的代码,则可以获得.success。