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

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

    Alternate Solution. Don't get messed up with onclick,buttons,server side and all.Just create a new form with different action like this.

    <form method=post name=main onsubmit="return validate()" action="scale_test.html">
    <input type=checkbox value="AC Hi-Side Pressure">AC Hi-Side Pressure<br>
    <input type=checkbox value="Engine_Speed">Engine Speed<br>
    <input type=submit value="Linear Scale" />
    </form>
    <form method=post name=main1 onsubmit="return v()" action=scale_log.html>
    <input type=submit name=log id=log value="Log Scale">
    </form>
    

    Now in Javascript you can get all the elements of main form in v() with the help of getElementsByTagName(). To know whether the checkbox is checked or not

    function v(){
    var check = document.getElementsByTagName("input");
    
        for (var i=0; i < check.length; i++) {
            if (check[i].type == 'checkbox') {
                if (check[i].checked == true) {
    
            x[i]=check[i].value
                }
            }
        }
    console.log(x);
    }   
    
    0 讨论(0)
  • 2020-11-29 20:08

    In this example, taken from

    http://www.webdeveloper.com/forum/showthread.php?t=75170

    You can see the way to change the target on the button OnClick event.

    function subm(f,newtarget)
    {
    document.myform.target = newtarget ;
    f.submit();
    }
    
    <FORM name="myform" method="post" action="" target="" >
    
    <INPUT type="button" name="Submit" value="Submit" onclick="subm(this.form,'_self');">
    <INPUT type="button" name="Submit" value="Submit" onclick="subm(this.form,'_blank');">
    
    0 讨论(0)
  • 2020-11-29 20:11

    In case you are up to HTML5, you can just use the attribute formaction. This allows you to have a different form action for each button.

    <!DOCTYPE html>
    <html>
      <body>
        <form>
          <input type="submit" formaction="firsttarget.php" value="Submit to first" />
          <input type="submit" formaction="secondtarget.php" value="Submit to second" />
        </form>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 20:11

    Simple and easy to understand, this will send the name of the button that has been clicked, then will branch off to do whatever you want. This can reduce the need for two targets. Less pages...!

    <form action="twosubmits.php" medthod ="post">
    <input type = "text" name="text1">
    
    <input type="submit"  name="scheduled" value="Schedule Emails">
    <input type="submit"  name="single" value="Email Now">
    </form>
    

    twosubmits.php

    <?php
    if (empty($_POST['scheduled'])) {
    // do whatever or collect values needed
    die("You pressed single");
    }
    
    if (empty($_POST['single'])) {
    // do whatever or collect values needed
    die("you pressed scheduled");
    }
    ?>
    
    0 讨论(0)
  • 2020-11-29 20:12

    Here's a quick example script that displays a form that changes the target type:

    <script type="text/javascript">
        function myTarget(form) {
            for (i = 0; i < form.target_type.length; i++) {
                if (form.target_type[i].checked)
                    val = form.target_type[i].value;
            }
            form.target = val;
            return true;
        }
    </script>
    <form action="" onSubmit="return myTarget(this);">
        <input type="radio" name="target_type" value="_self" checked /> Self <br/>
        <input type="radio" name="target_type" value="_blank" /> Blank <br/>
        <input type="submit">
    </form>
    
    0 讨论(0)
  • 2020-11-29 20:14

    HTML:

    <form method="get">
    <input type="text" name="id" value="123"/>
    <input type="submit" name="action" value="add"/>
    <input type="submit" name="action" value="delete"/>
    </form>
    

    JS:

    $('form').submit(function(ev){ 
    ev.preventDefault();
    console.log('clicked',ev.originalEvent,ev.originalEvent.explicitOriginalTarget) 
    })
    

    http://jsfiddle.net/arzo/unhc3/

    0 讨论(0)
提交回复
热议问题