Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在新窗口中打开表单结果_Javascript_Html_Forms_Search - Fatal编程技术网

Javascript 在新窗口中打开表单结果

Javascript 在新窗口中打开表单结果,javascript,html,forms,search,Javascript,Html,Forms,Search,我有两个按钮提交的表格 代码是: <form action="index.php" method="get"> <table cellpadding="2" cellspacing="3"> <tr> <td><input type="text" name="q" size="20" /></td> </tr> <tr>

我有两个按钮提交的表格

代码是:

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

我想要的是,如果你按下按钮,谷歌搜索结果将在一个新窗口中打开。我认为这应该用JavaScript来完成。


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

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

函数OpenGoogleSearch(){
var q=document.getElementById(“google”).value;
窗口打开(“http://google.com/search?q=“+q);
}
这确实需要稍微更改表格:

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


请注意,这只会在新窗口中打开谷歌搜索;如果您还想在新窗口中打开“站点搜索”,您应该添加
target=“_blank”
,正如Neal所说。

您确实可以通过javascript实现这一点:

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

函数OpenGoogleSearch(){
var q=document.getElementById(“google”).value;
窗口打开(“http://google.com/search?q=“+q);
}
这确实需要稍微更改表格:

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


请注意,这只会在新窗口中打开谷歌搜索;如果你想在一个新窗口中打开“站点搜索”,你应该添加
target=“_blank”
,正如Neal所说。

不是javascript,只需在表单标签上设置target=“_blank”。可能的重复不是javascript,只需在表单标签上设置target=“_blank”。可能的重复我认为他需要两个提交按钮的不同行为。第一个只是提交,后一个是询问谷歌给出的查询字段,这两个字段是相同的actions@Eineki因此,使用相同的输入创建两个不同的表单。我认为两个提交按钮需要不同的行为。第一个只是提交,后一个是询问谷歌给出的查询字段,这两个字段是相同的actions@Eineki因此,使用相同的输入生成2个不同的表单