How can I invoke multiple actions from a single fo

2019-02-27 16:30发布

问题:

I have a jsp file in which I have a form. It shows data about an "account" data structure we have. There's a button to remove the data from that data structure.

<form action="removeThisData.html" method="post">
  ... blah blah blah ...
  <input type="submit" value="Remove"/>
</form>

Now, there's a component that I want to be editable. Basically, I want that third blah to be turned into a date. Here's what I wish I could do.

<form action="removeThisData.html" method="post">
  ... blah blah blah ...
  <input type="submit" value="Change blah to date"/>
  <input type="submit" value="Remove"/>
</form>

Unfortunately, that button just acts upon the form action, which invokes the functionality mapped to "removeThisData.html".

How can I get two different behaviors from the same form? Is the invocation of multiple actions even possible within the same form? If it is not, is there a way to preserve all the information within a single scope or do I have to repeat the data/information in two different forms?

Thanks!

回答1:

You could rename removeThisData.html to, say, "manageThisData.html". You can examine the "value" of the submit button on the server-side and perform suitable action, based on the value, which would be one of "Remove" or "Change blah to date".



回答2:

You probably want the change date button to be a <input type="button" onclick="some_javascript();" /> rather than a submit button

Alternatively you could change the action with an onclick along the lines of onclick="this.form.action='changeDate.jsp';"

Another way still is to give name and value attributes to your submit buttons then have a switch in your JSP page logic on the value of this variable (i.e. if you named them all submit, you could see which button was clicked on by testing the value of the submit post argument)