【问题标题】:Opening form result in new window在新窗口中打开表单结果
【发布时间】:2011-12-09 12:01:36
【问题描述】:

我有一个带有两个按钮的表单提交。

代码是:

<form action="index.php" method="get">
    <table cellpadding="2" cellspacing="3">
        <tr>
            <td><input type="text" name="q" size="20" /></td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="site" value="site search" class="submit" />&nbsp;
                <input type="submit" name="google" value="google search" class="submit" />
            </td>
        </tr>
    </table>
</form>

我想要的是,如果您按下按钮,Google 结果将在新窗口中打开。我认为应该用 JavaScript 来完成。

【问题讨论】:

标签: javascript html forms search


【解决方案1】:
<form action="http://www.google.com/search" method="get" target="_blank">

【讨论】:

  • 我认为他需要两个提交按钮的不同行为。第一个只是提交,后者询问谷歌,因为两个操作的查询字段相同
  • @Eineki 所以使用相同的输入制作 2 种差异形式
【解决方案2】:

在 index.php 中,您可以根据按下的按钮执行单独的功能。例如:

<?php
if(isset($_GET('google'))&&isset($_GET('q'))){
     header('Location: http://www.google.ca/search?q=' . $_GET('q'));
}
if(isset($_GET('site'))&&isset($_GET('q'))){
     //function here
}
?>

【讨论】:

    【解决方案3】:

    你确实可以通过javascript实现:

    <script type="text/javascript">
        function OpenGoogleSearch() {
            var q = document.getElementById("google").value;
            window.open("http://google.com/search?q=" + q);
        }
    </script>
    

    这确实需要稍微改变表格:

    <form action="index.php" method="get">
        <input id="google" type="text" name="q" size="20" /></td>
        <input type="submit" name="site" value="site search" class="submit" />&nbsp;
        <input type="button" name="google" value="google search" class="submit" onclick="OpenGoogleSearch()" />
    </form>
    

    javascript 使用文本字段的 id(您应该添加)来获取输入的值。 google 搜索不使用提交,而是使用带有 onclick 属性的普通按钮,该按钮调用 javascript 函数。

    请注意,这只会在新窗口中打开 google 搜索;如果你也想在新窗口中打开“站点搜索”,你应该添加target="_blank",正如尼尔所说。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多