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

前端 未结 15 2260
暗喜
暗喜 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:14

    This might help someone:

    Use the formtarget attribute

    <html>
      <body>
        <form>
          <!--submit on a new window-->
          <input type="submit" formatarget="_blank" value="Submit to first" />
          <!--submit on the same window-->
          <input type="submit" formaction="_self" value="Submit to second" />
        </form>
      </body>
    </html>
    
    0 讨论(0)
  • 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

    <form action="mypage" method="get">
    
      <input type="submit" name="retry" value="Retry" />
      <input type="submit" name="abort" value="Abort" />
    
    </form>
    

    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.

    0 讨论(0)
  • 2020-11-29 20:18

    It is do-able on the server side.

    <button type="submit" name="signin" value="email_signin" action="/signin">Sign In</button>
    <button type="submit" name="signin" value="facebook_signin"  action="/facebook_login">Facebook</button>
    

    and in my node server side script

    app.post('/', function(req, res) {
    if(req.body.signin == "email_signin"){
                function(email_login) {...} 
            }
    if(req.body.signin == "fb_signin"){
                function(fb_login) {...}    
    
    
            }
        }); 
    
    0 讨论(0)
  • 2020-11-29 20:24

    On each of your buttons you could have the following;

    <input type="button" name="newWin" onclick="frmSubmitSameWin();">
    <input type="button" name="SameWin" onclick="frmSubmitNewWin();">
    

    Then have a few small js functions;

    <script type="text/javascript">
    function frmSubmitSameWin() {
    form.target = '';
    form.submit();
    }
    
    
    function frmSubmitNewWin() {
    form.target = '_blank';
    form.submit();
    }
    </script>
    

    That should do the trick.

    0 讨论(0)
  • 2020-11-29 20:25
    <form id='myForm'>
        <input type="button" name="first_btn" id="first_btn">
        <input type="button" name="second_btn" id="second_btn">
    </form>
    
    <script>
    
    $('#first_btn').click(function(){
        var form = document.getElementById("myForm")
        form.action = "https://foo.com";
        form.submit();
    });
    
    $('#second_btn').click(function(){
        var form = document.getElementById("myForm")
        form.action = "http://bar.com";
        form.submit();
    });
    
    </script>
    
    0 讨论(0)
  • 2020-11-29 20:26

    This works for me:

    <input type='submit' name='self' value='This window' onclick='this.form.target="_self";' />
    
    <input type='submit' name='blank' value='New window' onclick='this.form.target="_blank";' />
    
    0 讨论(0)
提交回复
热议问题