How can I specify which submit button to submit with?
The current example just submits the first submit button, with $("form").submit(); but how can I make it so it chooses the submit button by id or name?
<html>
<script>
$("form").submit();
</script>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" />
//other inputs
<input type="submit" value="Enter" name="enter" id="enter">
<input type="submit" value="Void" name="void" id="void">
<input type="submit" value="Refund" name="refund" id="refund">
</form>
</html>
-
You shouldn't have duplicated IDs btw.Fabrício Matté– Fabrício Matté2013年08月07日 22:38:42 +00:00Commented Aug 7, 2013 at 22:38
-
1You also should only have one id per elementhugomg– hugomg2013年08月07日 22:39:48 +00:00Commented Aug 7, 2013 at 22:39
-
@FabrícioMatté oops that was just a copy and paste typo, sorry.Arian Faurtosh– Arian Faurtosh2013年08月07日 22:40:11 +00:00Commented Aug 7, 2013 at 22:40
-
stackoverflow.com/questions/1365059/…Lord Midi– Lord Midi2013年08月07日 22:43:25 +00:00Commented Aug 7, 2013 at 22:43
-
No prob, back to the topic, a submit event triggered from JS does not send any submit button value from what I remember. I guess Blender's solution is your best shot, though I haven't tested it.Fabrício Matté– Fabrício Matté2013年08月07日 22:44:31 +00:00Commented Aug 7, 2013 at 22:44
3 Answers 3
Simulate a click to that element:
$("#circle2").click();
Also, you don't need action="<?=$_SERVER['PHP_SELF']?>". Forms submit to the current page by default.
Comments
First of all, why do you want to submit the same form with 3 different buttons?
It is a bad structure. Your code also has all the 3 buttons with the "id" attribute which is included in the <input> tag twice.
Based on your question, I could figure out you would want the submit button to say different things under different conditions.
Have a single button like this :
<input type="submit" name="submit" value="Enter">
You could always change what your button says, or how it looks like with JQuery :
if(condition){
$('#submit').val('.....');
// You can also change more stuff as you want.
}
Then you would want to submit the form
$('#submit').click(function(e)){
e.preventDefault();
$('form').submit();
}
4 Comments
name and value attributes of the button. Here, even if you had JavaScript turned off, you could enter, void and refund from the same form.By id
You can select the element by id easily with $('#my_btn') and you can click on it using the jQuery method click().
By name (or any other attribute
Other attributes are a bit harded (but not complex)
You select the element with $('input[name=some_name]')
Examlpe using your code
Here is an example which shows how you can get elements by name and click on them, click the submit buttons to see what happens: http://jsfiddle.net/nabil_kadimi/99v93/2/