首先:我试着在网上搜索答案,我发现了大约20个代码示例。但是我失败了。但仍然没有任何效果。
我正在做一个简单的聊天,我需要通过ajax提交数据后清理我的表单。下面是代码:
<form id="ChatFrom" class="chatMessageField" action="Amess.php" method="post">
<input class="chatMessageField" type="text" name="mess" /><br />
<input class="chatMessageBtn" type="button" value="Отправить" onclick="SendForm();" />
</form>
<script type="text/javascript" src="http://scriptjava.net/source/scriptjava/scriptjava.js"></script>
<script type="text/javascript">
function SendForm() {
$$f({
formid:'ChatFrom',
url:'Amess.php',
});
}
</script>
$("#ChatFrom")[0].reset()
// or
$("#ChatFrom").get(0).reset()
你可以这样做:
function SendForm() {
$.post( "Amess.php", function( data ) {
resetFields();
});
}
function resetFields(){
$(':input')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
}
希望有帮助!
$(".chatMessageField").val (");是一种简单的方法,但是您必须对所有字段单独执行。更好的解决方案是为所有输入提供相同的类名,并一次清除它们。或者直接使用jquery reset()