HTML form with two submit buttons and two “target” attributes

前端 未结 15 2271
暗喜
暗喜 2020-11-29 20:02

I have one HTML

.

The form has only one action=\"\" attribute.

However I wish to have two different target=\"\" attribu

15条回答
  •  清酒与你
    2020-11-29 20:15

    I do this on the server-side. That is, the form always submits to the same target, but I've got a server-side script who is responsible for redirecting to the appropriate location depending on what button was pressed.

    If you have multiple buttons, such as

    
    
      
      
    
    
    

    Note : I used GET, but it works for POST too

    Then you can easily determine which button was pressed - if the variable retry exists and has a value then retry was pressed, and if the variable abort exists and has a value then abort was pressed. This knowledge can then be used to redirect to the appropriate place.

    This method needs no Javascript.

    Note : that some browsers are capable of submitting a form without pressing any buttons (by pressing enter). Non-standard as this is, you have to account for it, by having a clear default action and activating that whenever no buttons were pressed. In other words, make sure your form does something sensible (whether that's displaying a helpful error message or assuming a default) when someone hits enter in a different form element instead of clicking a submit button, rather than just breaking.

提交回复
热议问题