我可以使用 jQuery 提交包含多个提交按钮的表单吗?

jqueryweb developmentfront end technology更新于 2026/2/6 3:07:17

是的,您可以提交包含多个提交按钮的表单。为所有按钮添加自定义点击处理程序,并检查哪个按钮被点击了。

示例

<!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>

相关文章