可以使用 jQuery 在具有多個提交按鈕的表單中提交表單嗎?
是的,可以使用多個提交按鈕在表單中提交表單。為所有按鈕新增自定義單擊處理程式,並檢查單擊了哪個按鈕。
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#myform button").click(function (ev) {
ev.preventDefault()
if ($(this).attr("value") == "button1") {
alert("First Button is pressed - Form will submit")
$("#myform").submit();
}
if ($(this).attr("value") == "button2") {
alert("Second button is pressed - Form did not submit")
}
});
});
</script>
</head>
<body>
<form id="myform">
<input type="email" name="email" placeholder="Enter email" /><br>
<button type="submit" value="button1">First Button</button>
<button type="submit" value="button2">Second Button</button>
</form>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP