jquery提交表单
jQuery submit() function triggers when a user tries to submit a form. jQuery submit form method attaches a handler, which executes when the submit event is fired.
当用户尝试提交表单时,jQuery Submit()函数将触发。 jQuery提交表单方法附加了一个处理程序,该处理程序在触发Submit事件时执行。
jQuery提交表单 (jQuery submit form)
The syntax for submit form using jQuery submit() function:
使用jQuery Submit()函数提交表单的语法:
- submit() 提交()
This signature is used without any arguments. This method is a shortcut for .trigger( "submit" )
.
该签名不带任何参数。 此方法是.trigger( "submit" )
的快捷方式。
- submit(handler) 提交(处理程序)
The handler
is a function, which is executed when the form is submitted. This method is a shortcut for .on( "submit", handler )
.
handler
是一个函数,在提交表单时执行。 此方法是.on( "submit", handler )
的快捷方式。
jQuery提交表单示例 (jQuery submit form example)
Following example demonstrates submit() function usage for form submission using jQuery.
以下示例演示了使用jQuery提交表单的Submit()函数用法。
<!DOCTYPE html>
<html>
<head>
<title>jQuery Submit</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<style>
p {
margin: 0;
color: blue;
}
div,p {
margin-left: 10px;
}
.spanSuccess {
color: green;
}
</style>
</head>
<body>
<form action="javascript:alert( 'success!' );">
<div>
Name : <input type="text" name="Name" value="journalDev">
<input type="submit" value="Submit">
</div>
</form>
<span class="spanSuccess"></span>
<script>
$( "form" ).submit(function( event ) {
$( "span" ).text( "Submitted Successfully..." ).show();
event.preventDefault();
});
</script>
</body>
</html>
In above jQuery form submit example, you can see that the submit() method is triggered when you click on the Submit button. A text message is displayed as “Submitted Successfully…”.
在上面的jQuery表单Submit示例中,您可以看到单击Submit按钮时触发了commit()方法。 一条文字消息显示为“ Submittedfully…”。
jQuery表单提交演示 (jQuery form submit demo)
You can try it yourself by submitting below form.
您可以提交以下表格自行尝试。
You may have noticed event.preventDefault()
in the above code. You will see an alert, if you try the above example after removing this line. We used alert()
method as the default action. So the event.preventDefault()
is required in the code to prevent this default action.
您可能已经在上面的代码中注意到event.preventDefault()
。 如果删除此行后再尝试上述示例,则会看到警报。 我们使用alert()
方法作为默认操作。 因此,代码中需要使用event.preventDefault()
来阻止此默认操作。
That’s all for a quick jQuery form submit example. You can read learn jQuery by going through jQuery Tutorial.
这就是快速jQuery表单提交示例的全部内容。 您可以阅读jQuery Tutorial来学习jQuery。
Reference: API Doc
参考: API文档
jquery提交表单