How to submit 2 forms in one page with a single submit button

后端 未结 4 1102
天涯浪人
天涯浪人 2020-11-28 12:07

I\'ve created a php page with two forms but I would like to have only one submit button for both forms. the forms have the ids firstform &

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 12:59

    • Set the "target" attribute on the first form to "_blank"
    • Set the "action" attribute on the first form to "#close" (replace "close" with whatever you want.
    • Have a script on the page that checks if the document.location.hash is "close" if it is window.close()

    Here's the jsfiddle to demonstrate.

    http://jsfiddle.net/TqhPr/18/

    HTML



    JavaScript

    $(document).ready(function() {
        $("#both").click(function() {
            document.getElementById("f1").submit();
            document.getElementById("f2").submit();
        });
        if(document.location.hash == "#close") {
            alert("closing the window caused by posting the first form");
            window.close();
        }
        if(document.location.hash) {
            alert(document.location.hash);
        }
    });
    

提交回复
热议问题