我可以用 jQuery 提交帶有多個提交按鈕的表單嗎?


可以,你可以使用 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>

更新日期:20-6-2020

2 千+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始
廣告