javascript 如何处理一个表单上的两个提交按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3321585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:50:59  来源:igfitidea点击:

How do I handle Two Submit buttons on One Form?

javascriptjqueryhtmlasp.net-mvc

提问by MrM

I have two submit buttons and one form. How do I check what submit button was selected in my jquery code?

我有两个提交按钮和一个表单。如何检查在我的 jquery 代码中选择了哪个提交按钮?

<% using (Html.BeginForm("UserInfo", "Home", FormMethod.Post, new { id  = "formNext" })) { %> 
.... 
<input id="submitHome" type="submit" name="goHome" value="Home" />  
<input id="submitNext" type="submit" name="getNext" value="Next" /> 
<% } %>


$(document).ready(function() {       
$('#formNext').submit(function() {      
        //Code Does not work but looking at something like this...
        //$('#submitHome').click(function() {
        //      navigate to Home;
        //});
        //$('#submitNext').click(function() {
        //      return true;
        //});
    });
});

回答by heisenberg

$('#submitHome').click(function() {
      //navigate to Home;
});
$('#submitNext').click(function() {
      return true;
});

These should work if you pull them outside of the form.submit(). (right now those handlers are being attached after the form is submitted, which is too late since the click has already occurred)

如果您将它们拉到 form.submit() 之外,这些应该可以工作。(现在这些处理程序是在提交表单后附加的,因为点击已经发生为时已晚)

回答by Jose Rojas

You can try something like this

你可以试试这样的

$(function() {
 var buttonpressed;
 $('input[type=submit]').click(function() {
      buttonpressed = $(this).attr('name')
 })
 $('form').submit(function() {
      alert('button clicked was ' + buttonpressed)
        buttonpressed=''
    return(false)
 })
})

Source: https://forum.jquery.com/topic/determining-which-of-two-submit-buttons-were-clicked-in-a-single-form

来源:https: //forum.jquery.com/topic/determining-which-of-two-submit-buttons-were-clicked-in-a-single-form

回答by Marwelln

This should work:

这应该有效:

<% using (Html.BeginForm("UserInfo", "Home", FormMethod.Post, new { id = "formNext" })) { %>
....
<input id="submitHome" type="submit" name="goHome" value="Home" />
<input id="submitNext" type="submit" name="getNext" value="Next" />
<% } %>


$(document).ready(function() {
  $('#submitHome').click(function(e) {
    e.preventDefault(); // prevent the page to do the usuall onclick event (from reloading the page)
    // navigate to Home;
  });
  $('#submitNext').click(function(e) {
    e.preventDefault();
    // return true;
  });
});

回答by Tobiasopdenbrouw

$(function(){
  $(".submitButton").click(function(e){
    alert($(this).attr("name"));
  });
});?

Working demo behind the link.

链接背后的工作演示。

回答by PradeepN

Create two action methods and that handles two post seperately that takes formcollection as param. Add two input with type=button and bind the click event with $.post to your action method with values from the form controls you need for that post.

创建两个动作方法,分别处理两个以 formcollection 为参数的帖子。添加两个带有 type=button 的输入,并将带有 $.post 的单击事件绑定到您的操作方法,其中包含来自该帖子所需的表单控件的值。