简体   繁体   中英

Handling 2 submit buttons in one form

I have read many articles explaining how to handle 2 submit buttons in a form but none of them have worked for me.

form :

<form id="myForm">
<input type="submit" name="btngroup" value="remove" />
<input type="submit" name="btngroup" value="activate" />
</form>

js:

$('#myForm').submit(function (e) {
    e.preventDefault();
    var form = $(this);
    $.ajax({
            url: '/Admin/removeDocPermanently',
            type: 'POST',
            DataType: "html",
            data: form.serialize()
    });
});

controller

[HttpPost]
public ActionResult removeDocPermanently(string btngroup, FormCollection form)
{  
   //btngroup does not get the value of the clicked submit button
}

Is this possible to get the pressed submit button in the js submit function or the controller?

You can hold the submit object on submit button click and use it in submit funciton.

var submitCommand;
$('input:submit').click(function(){
 submitCommand = $(this);
});


$('#myForm').submit(function (e) {
       alert(submitCommand.val());
       //your code
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM