提交表单后运行函数

时间:2019-09-09 19:35:50

标签: javascript html

我想在提交表单后 执行一些javascript。 (我希望它清除某些输入字段的内容,并可能将先前提交的一些信息打印到html页面上)

我已经在使用<input type="submit" onclick="appendRedundancy()" value="Submit">来让计算机在发送表单之前进行一些处理。如果我告诉appendRedundancy()清理表单,我担心它会在发送之前清理表单。

如果我在<script> cleanForm(); </script>之后添加<form>...</form>

它在人填写表格之前执行它。

我如何规定事件的顺序?

非常感谢!

1 个答案:

答案 0 :(得分:0)

提交事件

<input type="submit" onsumbit="appendRedundancy()" value="Submit">

示例:

<!DOCTYPE html>
<html>
<body>

<p>When you submit the form, a function is triggered which alerts some text.</p>

<form action="/action_page.php" onsubmit="myFunction()">
  Enter name: <input type="text" name="fname">
  <input type="submit" value="Submit">
</form>

<script>
function myFunction() {
  alert("The form was submitted");
}
</script>

</body>
</html>

另一种方法是您通过ajax发布并通过成功功能获取它

   $(document).ready(function(){
 $("p").click(function(){

$.ajax({

type: 'POST',
url: 'url.php',
data: $("#your_form").serialize(),
success: function(data) {
//do something here 
 }
});
});
 });