HTML5 button.formaction Attribute not working Outside of <form>

Why does the button.formaction attribute not work outside of the <form> ?

This fails:

 <button type="submit" formaction="a.jsp">a<button> <button type="submit" formaction="b.jsp">b<button> <button type="submit" formaction="c.jsp">c<button> 

However, this works for me:

 <form action="foo.jsp" method="post"> <button type="submit" formaction="a.jsp">a<button> <button type="submit" formaction="b.jsp">b<button> <button type="submit" formaction="c.jsp">c<button> </form> 

But in my application, it makes no sense for me to use a form, since I never want to access foo.jsp , and I don't have a default value. Is there a way to make the format work without using a dummy value or by default for the a.jsp form?

+7
source share
1 answer

In general, form elements are used to send data to the server. It covers elements that define, for example, input or button elements. If you add the name and value attribute to your button elements, you will send this name-value pair to your server.

If you do not need to send any (additional) data to your server, just use the link elements and use them as buttons using CSS, if you want:

 <a href="a.jsp" class="btn">a</a> <a href="b.jsp" class="btn">b</a> <a href="c.jsp" class="btn">c</a> 

formaction attribute

Using the formaction attribute formaction you can specify multiple submit URLs for form one . Since the action attribute is no longer required for the form element, you can only define submit URLs in the formaction button. When the form is submitted, the browser first checks the formaction attribute; If this is not present, it begins to search for the action attribute in the form element. Thus, the action of the form is something like a backup or default, it is not required:

 <form method="post"> <input type="text" name="my-data" value="my data"/> <button type="submit" formaction="a.jsp">a</button> <button type="submit" formaction="b.jsp">b</button> <button type="submit" formaction="c.jsp">c</button> </form> 

If you use the form attribute to reference a linked form (id value), you can even put a button outside the form element:

 <form method="post" id="myForm"> <input type="text" name="my-data" value="my data"/> </form> <button type="submit" formaction="a.jsp" form="myForm">a</button> <button type="submit" formaction="b.jsp" form="myForm">b</button> <button type="submit" formaction="c.jsp" form="myForm">c</button> 
+14
source

Source: https://habr.com/ru/post/970483/


All Articles