dongnuo2879 2014-04-28 18:35
浏览 91
已采纳

表单有两个提交按钮做两个不同的事情

I have a form that I have two buttons on. One button should take the user to one php script and the other button would take the user two a different php script. However for some reason the buttons aren't doing anything. Here is the code.

    <script language="Javascript">
function OnButton1()
{
document.contactform.action = "../scripts/email-info.php"
// document.Form1.target = "_blank";    // Open in a new window

document.contactform.submit();             // Submit the page

return true;
}

function OnButton2()
{
document.contactform.action = "../scripts/email-info2.php"
//document.contactform.target = "_blank";    // Open in a new window

document.contactform.submit();             // Submit the page

return true;
}
</script

Then here is the actual form code:

<form id="contact-form" name="contactform" method="post">
<fieldset>
<div id="holder">
<div id="formLeft">
<div class="txtlabel">Name* </div><div class="input-bg"><input type="text" 
name="name"       id="name" required></div>
</div>
</div>
<div id="msgbox">
<div class="txtlabel">Tell Us About Your Business Needs</div>
<div class="message-bg">
<textarea name="message" id="message" rows="9" cols="56" required></textarea><input 
name="formpage" type="hidden" value="<?=$pagename;?>" />
</div></div><div style="clear:both"></div><br /><br />
<input src="/submitbtn.jpg"  
name="submit" value="View Now" class="submit-button" onclick="OnButton1();"/>
<input src="/submitbtn.jpg"  
name="submit" value="Download Now" class="submit-button2" onclick="OnButton2();" />
</fieldset>

</form>

I've removed some of the submission fields to make it more easily viewable. But I get nothing when I click either button...Any thoughts??

  • 写回答

3条回答 默认 最新

  • dsiimyoc804955 2014-04-28 18:46
    关注

    problem is in input:

    <input src="/submitbtn.jpg"  
      name="submit" value="View Now" class="submit-button" onclick="OnButton1();"/>
    

    When you have a form:

    document.contactform.submit
    

    Javascript returns the input with name submit, not the submit function.

    You could change the name of the input:

    <input src="/submitbtn.jpg"  
      name="yourName" value="View Now" class="submit-button" onclick="OnButton1();"/>
    

    Also, your inputs are not buttons, check this.

    Update

    This question mentions HTML5 formaction attribute:

    <form action=#">
        <buton formaction="script-1.php">submit one</button>
        <buton formaction="script-2.php">submit two</button>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能