Form with 2 submit buttons

≡放荡痞女 提交于 2019-12-11 09:06:11

问题


I have one form with 2 submit buttons. The form generates a URL.

The 2 outputs of the form are:

  1. It writes a URL to a hidden div which displays when they click the Preview button, and
  2. It launches the same URL in a new window when they click the Open button.

I am able to write to the hidden DIV, so the Preview function works perfectly.

My problem is:

I am not able to get the window.open function to work correctly with the Open button.

When I click the button, I get "http://www.my_current_domain.com/undefined" when I have initially entered a different domain in the "firstfield" input area.

Here is a sample of my script:

<script type="text/javascript">
    function myfunction() {
        var firstfield = document.getElementById('firstfield').value;
        ...repeated.. for 7 more fields...
        /* Base 64 Encode the username and password */
        var UserPassword64Encoded = User + ":" + Password;
        var UserPassword64Encoded = btoa(UserPassword64Encoded);
        /* here is where I assemble the URL */
        document.getElementById('results').innerHTML = firstfield + "?some_static_info=" +
        ... repeated 7 more times...;
        document.getElementById('results').style.display = "block";
        return false;
    }
</script>

Here is a sample of my form:

<form name="myForm" onSubmit="return myfunction();">
    <input  type="text" id="firstfield" name="firstfield"/>
    ... repreated 7 more times...
    <input type="submit" value="Preview"/>
    <input type="submit" onClick="window.open(myForm.results)" value="Open"/>
</form>
/* Hidden DIV */
<div id="results">
</div>

I've been working at just this one issue for 2 days now and am now going in circles.

Any advice would be appreciated.

Best Regards, Dennis Hall


回答1:


You can't get 'results' in the manner you're attempting. You'll need to try getting it via the ID reference, something like the following:

<form name="myForm" onSubmit="return myfunction();">
    <input  type="text" id="firstfield" name="firstfield"/>
    ... repreated 7 more times...
    <input type="submit" value="Preview"/>
    <input type="submit" onClick="window.open(document.getElementById('firstField').value + '/' + document.getElementById('results').value);" value="Open"/>
</form>
/* Hidden DIV */
<div id="results">
</div>

EDIT: And to make sure window.open doesn't think its a relative URL, ensure it starts with a protocol, eg http:// or https://.



来源:https://stackoverflow.com/questions/30852807/form-with-2-submit-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!