我使用php,ajax,javascript,jquery在html中創建表單。輸入數據後,其在數據庫中更新但輸入欄未更清楚。我將在java腳本中清除()。但它不工作,請幫我在下面的代碼Html input field not clear,used clear()but not working
的index.html
<html>
<head><title>Registration form</title></head>
<body>
<form id="myForm" action="process.php" method="POST">
Username: <input type="text" name="username" id="username"> <br />
Password: <input type="password" name="pass" id="pass"> <br />
First name: <input type="text" name="fname"> <br />
Last name: <input type="text" name="lname"> <br />
<button id="submit" name="submit">Register</button>
</form>
<div id="ack">Acknowledge</div> \t
<script type="text/JavaScript" src="script/jquery-1.11.3.min.js"></script>
<script type="text/JavaScript" src="script/my_script.js"></script>
</body>
</html>
my_script.js
$("#submit").on('click',function(e){
\t e.preventDefault();
\t $.post(
$("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info,status,xhr){
$("#ack").empty();
$("#ack").html(info);
clear();
\t \t \t });
$("#myForm").submit(function(){
return false;
\t });
});
function clear(){
$("#myForm:input").each(function(){
$(this).val("");
});
}
我會將'click'處理程序中的邏輯移動到'submit'一個,而不是返回false。只需將e.preventDefault()保留在頂部即可。 – mariocatch
將$(「#myForm:input」)更改爲$(「#myForm:input」) – Alex
[This](http://www.sitepoint.com/jquery-function-clear-form-data/)可以幫助您。 – Alex