Chandishwar Chitmal
Chandishwar Chitmal

Reputation: 1

how to submit the values in the form in javascript when there are 2 submit values?

I have one form where there are 2 inputs those are submit type of inputs like this.

<form>
    <input type="text" name="payee" value="">
    <input type="text" name="amount" value="">
    <input type="text" name="date" value="">
    <input type="submit" name="deposit" value="Distribute">
    <input type="submit" name="distribute" value="Deposit">
</form>

In jQuery like this:

$("form submit").click(function() {
    // i wrote code.
}

If I click on the deposit button some action should occur. If I click on the distribute button some other action should occur.

Upvotes: 0

Views: 178

Answers (4)

bruno
bruno

Reputation: 91

Try to use the jQuery submit() function, like this:

$('#deposit').submit(function(){
  //Code to run
});

It's better because you can cancel the client event, if some field is wrong.

http://api.jquery.com/submit/

You don't need a plugin to do it, however there's a lot of them: http://www.designyourway.net/blog/resources/55-jquery-form-plugins-to-download-and-use/

Upvotes: 0

ankur
ankur

Reputation: 4733

You can add a custom attribute on your buttons in document.ready function and on click of the button you can identify which button has posted an request to form.

Example to add custom attribute

<input type="submit" id="deposit" value="Distribute">
<input type="submit" id="distribute" value="Deposit">


$(function () {
$("#deposit").attr('isDeposit','1');
$("#distribute").attr('isDeposit','0');

});

and on submit click

$("form submit").click(function()
{
var identifyButton = $(this).attr('isDeposit');
}

some thing like this.

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148714

insert another input type :

<input type="hidden" name="hf" id="hf" value="">

and

$("form :submit").click(function() {
  if ($(this).attr('id')=='distribute') $("#hf").val('sent by distribute');
else $("#hf").val('sent by deposit');
}

and in the server you can see who send by reading the hiddenField value ( hf)

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337701

First of all you need to change your submit inputs to buttons (or at least one of them) as 2 submit buttons in 1 form is invalid. Then give each button its' own Id.

<input type="button" name="deposit" value="Distribute" id="distribute">
<input type="button" name="distribute" value="Deposit" id="deposit">

Then, in jQuery you can then use the Id of each button to run specific code when it is clicked, like this:

$("#distribute").click(function() {
    // code to run when distribute is clicked
}
$("#deposit").click(function() {
    // code to run when deposit is clicked
}

Upvotes: 5

Related Questions