1

我有一个带有按钮的搜索栏,可以搜索另一个站点上的内容。我让它正常运行(它在新选项卡中的另一个站点上搜索)但是每当我在搜索栏中单击输入时,我正在重新加载的页面。起初它从按回车键并单击搜索重新加载,然后我将输入类型更改为按钮而不是提交。当点击按钮时,这会重新加载,但是当按下回车键时,页面会重新加载。此外,它仅在单击搜索栏时才会重新加载,而不仅仅是在任何时候。必须为输入键选择搜索栏才能重新加载页面。

html:

<div id="search-3" class="widget widget_search">
    <h3 align="center">Search the other site!</h3>
    <form method="post" name="searchform" id="searchform">

      <span class="searching">
        <input type="text" value="" name="s" id="s" />
      </span>
      <input class="submit" type="button" Onclick="SearchBlog();" value="Search" />

    </form>
</div>

javascript:

function SearchBlog()
{
    var searchQuery = document.searchform.s.value;
    if(searchQuery != ""){
        var destination = "website.com/s" + searchQuery;
        window.open(destination, "_blank");
    }
}

我希望能够使用回车键进行搜索,但我不希望它重新加载页面。

4

2 回答 2

2

那是因为您要提交到一个不存在的页面。

onsubmit在您的<form>声明中使用:

<form method="POST" name="searchform" id="searchform" onsubmit="SearchBlog();">
   ...
</form>

然后确保您return false;在 SearchBlog() 函数中。你应该很高兴。

于 2012-09-13T15:47:31.797 回答
0

试试这个:

<input class="submit" type="button" Onclick="return SearchBlog();" value="Search" />

function SearchBlog()
{
    var searchQuery = document.searchform.s.value;
    if(searchQuery != ""){
        var destination = "website.com/s" + searchQuery;
        window.open(destination, "_blank");
    }
    return false;
}
于 2012-09-13T15:47:48.697 回答