问题
I have one form with 2 submit buttons. The form generates a URL.
The 2 outputs of the form are:
- It writes a URL to a hidden div which displays when they click the Preview button, and
- 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