打开表单会出现新窗口

Opening form result in new window

本文关键字:窗口 新窗口 表单      更新时间:2023-09-26

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

代码是:

<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>

我想要的是,如果你按下按钮,谷歌结果将在一个新的窗口打开。

<form action="http://www.google.com/search" method="get" target="_blank">

在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
}
?>

您确实可以通过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",就像Neal说的。