I have small html calculator, where you can enter two numbers, with two separate buttons to either add or subtract them, like this:
<form method="post" action="<%= actionURL %>" >
<input type="text" name="x" value="${x}"/>
<input type="text" name="y" value="${y}"/>
<input type="submit" name="add" value="add"/>
<input type="submit" name="subtract" value="subtract"/>
</form>
I would like to replace the two buttons with a select
element, that would submit the form when the user chooses one of the options. So far I have the following:
<form method="post" action="<%= actionURL %>" >
<input type="text" name="x" value="${x}"/>
<input type="text" name="y" value="${y}"/>
<select name=operation onChange="this.form.submit()">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
</select>
</form>
Now I get a dropdown box with the options "Add" and "Subtract". Whenever I choose "Subtract", the form gets submitted, and I can check the operation
parameter of the request back on the server. But clicking on the dropdown box and then on "Add" doesn't fire the onChange
event, since "Add" is already selected, and so the form is not submitted. Is there any way I can submit the form, when the user chooses the already selected option?