在消息发送后清除表单输入
Clear form input after message has been sent
我试图在发送表单后清除表单消息输入字段。尽管它在数据有机会被发送之前清除数据。下面是我的代码:
<script>
$(function () {
$('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.
});
e.preventDefault();
});
});
</script>
<script>
$('#SendForm').on('click', function () {
$('#SendForm').find('input:text').val('');
$('input:checkbox').removeAttr('checked');
});
</script>
形式:
<form id="SendForm" class="SendMsg" role="form" method="post" action="sendmessagefriend.php">
<input type="text" id="s_firstname" name="s_firstname" class="MsgInputHidden" value="<?= $_SESSION["user"]["firstname"] ?>" />
<input type="text" id="s_lastname" name="s_lastname" class="MsgInputHidden" value="<?= $_SESSION["user"]["lastname"] ?>" />
<input type="text" id="sender" name="sender" class="MsgInputHidden" value="<?= $_SESSION["user"]["id"] ?>" />
<input type="text" id="r_firstname" name="r_firstname" class="MsgInputHidden"value="<?= $FriendName->firstname ?>" />
<input type="text" id="r_lastname" name="r_lastname" class="MsgInputHidden"value="<?= $FriendName->lastname ?>" />
<input type="text" id="recipient" name="recipient" class="MsgInputHidden" value="<?= $FriendName->id ?>" />
<input type="text" id="ip" name="ip" class="MsgInputHidden" value="<?= $_SERVER["REMOTE_ADDR"] ?>" />
<input type="text" id="date" name="date" class="MsgInputHidden" value="<?= date('Y-m-d') ."'n" ?>" />
<?php $now = time(); $utc_time = $now - intval(date('Z', $now)); ?>
<input type="text" id="time" name="time" class="MsgInputHidden" value="<?= '' . date('H:i:s', $now) . '' ?>" />
<input id="message" type="text" name="message" style="width: 100%; overflow: scroll;">
<input id="submit" class="submit" type="submit" name="submit" value="Submit" />
</form>
用这个
代替点击处理程序$('#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();
});
相关文章:
- 如何在使用AngularJS更改下拉值时清除表单中的数据
- 在Chrome中重新加载页面后清除表单输入值
- Angularjs:提交后清除表单无效
- 我无法使用 .reset() 清除表单
- 我无法从引导模式中清除表单数据
- 如何在我的create.js.erb中重置/清除表单
- 如何使用javascript删除项目和清除表单
- 当没有表单名称时,如何在AngularJS中清除表单
- 如何不清除表单jQuery中禁用的输入
- 使用 jQuery 重置/清除表单
- 提交 angularjs 后清除表单
- 在 cakephp 1.3 中提交后清除表单
- MVC 3 剃刀无法使用验证类清除表单值
- JavaScript -- 提交值后清除表单
- 如何编辑我的 js,以便在输出输出之前清除表单
- 提交后清除表单中的文本字段
- Chrome扩展-点击清除表单文本
- 提交时未清除表单
- 如何在验证后清除表单
- 单击运行清除表单的JS函数的按钮后,视图无法恢复