[英]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('Ym-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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.