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

2K+ 次觀看

職業生涯突飛猛進

完成課程,獲取證書

開始
廣告
© . All rights reserved.