且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JavaScript处理带有多个提交按钮的表单提交

更新时间:2022-12-11 15:03:16

您可以将自定义点击处理程序附加到所有按钮,这样您可以在提交表单之前检查单击了哪个按钮:

you can attach a custom click handler to all buttons, and that way you can check which button is clicked before submitting the form:

在线示例

$("#my-form button").click(function(ev){
    ev.preventDefault()// cancel form submission
    if($(this).attr("value")=="button-one"){
        //do button 1 thing
    }
    // $("#my-form").submit(); if you want to submit the form
});