带2个提交按钮的表单

带2个提交按钮的表单

本文介绍了带2个提交按钮的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个提交按钮的表单。该表单生成一个URL。



表单的2个输出是:


  1. 它将一个URL写入一个隐藏的div,当它们点击预览按钮时显示,并且
  2. 当它们点击打开按钮时,它会在新窗口中启动相同的URL。

我可以写入隐藏的DIV,因此预览功能可以完美地工作。

我的问题是:

我无法使window.open函数正常使用Open按钮。



当我点击按钮时,我会收到 http://www.my_current_domain.com/undefined当我最初在firstfield输入区域中输入了一个不同的域时。



以下是我的脚本示例:

 < script type =text / javascript> 
函数myfunction(){
var firstfield = document.getElementById('firstfield')。value;
...重复.. for 7 more fields ...
/ * Base 64编码用户名和密码* /
var UserPassword64Encoded = User +:+ Password;
var UserPassword64Encoded = btoa(UserPassword64Encoded);
/ *这里是我组装URL的地方* /
document.getElementById('results')。innerHTML = firstfield +?some_static_info =+
...重复7次以上。 ..;
document.getElementById('results')。style.display =block;
返回false;
}
< / script>

以下是我的表单样本:

 < form name =myFormonSubmit =return myfunction();> 
< input type =textid =firstfieldname =firstfield/>
...表示7次以上...
< input type =submitvalue =Preview/>
< input type =submitonClick =window.open(myForm.results)value =Open/>
< / form>
/ *隐藏的DIV * /
< div id =results>
< / div>

现在我一直在为这个问题工作2天,现在正在圈子里。



任何建议,将不胜感激。



最好的问候,
丹尼斯霍尔

解决方案

您无法以您尝试的方式获得结果。您需要尝试通过ID引用来获取它,如下所示:

 < form name =myForm onSubmit =return myfunction();> 
< input type =textid =firstfieldname =firstfield/>
...表示7次以上...
< input type =submitvalue =Preview/>
< input type =submitonClick =window.open(document.getElementById('firstField')。value +'/'+ document.getElementById('results').value);值= 打开/>
< / form>
/ *隐藏的DIV * /
< div id =results>
< / div>

编辑:并确保window.open不认为它一个相对的URL,确保它以协议开始,例如http://或https://。

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

解决方案

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://.

这篇关于带2个提交按钮的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:24