Husain a écrit :
Hello.
I am new to Javascript and JSP. I would like to have multiple submit
buttons in a form. Each button has a different action? How can I go
about doing that? How can I obtain the button's name or id?
Usual way of doing :
- All submit-buttons have same name
- each button has its own value according to some action
- code server-side uses the name of submit button to know
what action to serve (the submit-button's value)
with the other elements of the form
- javascript uses :
1) an onclick attributed to the button
- most simple :
to give the value of button to a global variable
<input type=submit value="Do That" name="send"
onclick="ToDo=this.value">
- or a function to do somethin with the elements of the form
2) the attribute 'onsubmit' given to the tag 'form'
That can give :
<html>
<script type="text/javascript">
var ToDo = '';
function validate () {
return confirm('Ok for this action : '+ToDo+' ?');
}
</script>
<form action="javascript:alert('message sent')"
onsubmit="return validate();">
<p><input type=submit name="send" value="First action"
onclick="ToDo=this.value">
<p><input type=submit name="send" value="Second action"
onclick="ToDo=this.value">
<p><input type=submit name="send" value="Third action"
onclick="ToDo=this.value">
</form>
</html>