MOSS 2007 最简单的自定义搜索框 SearchBox

 项目有个需要就是对搜索中心进行定制,要求普通搜索,只对标题搜索,高级搜索才对内容搜索。

这个问题好长一段时间不能解决,原本打算重新用搜索的API,重新开发所有搜索的webpart,最后找到一个比较简单的方法:

1 通过研究,可以用JS实现,搜索框与原来的搜索框样式一样。

2 添加一个自定义的搜索结果页:  /Search/customResults.aspx,

编写核心搜索的xslt,屏蔽掉如下代码,就是不显示内容搜索的内容:

    <div class="srch-Description">
      <xsl:choose>


      <!--
        <xsl:when test="hithighlightedsummary[. != '']">
          <xsl:call-template name="HitHighlighting">
            <xsl:with-param name="hh" select="hithighlightedsummary" />
          </xsl:call-template>
        </xsl:when>

       --> 

             <xsl:when test="description[. != '']">
          <xsl:value-of select="description"/>
        </xsl:when>
      </xsl:choose>
    </div >

3 高级搜索中,结果页还用原来的结果页显示/Search/results.aspx

4 分别在/Search/results.aspx,/Search/customResults.aspx,/Search/default.aspx页面删除原来的搜索框,

添加一个内容编辑器Web部件,添加如下HTML代码,即可。

通过JS会自动在普通搜索中,添加“k=title:guoqiang”,这样就实现了只对标题guoqiang进行搜索了。

  <script type="text/javascript">
        function CustomSearch() {
            var key = document.getElementById("customInputKeywords").value;
            var searchUrl = "/Search/customResults.aspx?k=title%3A" + key;
            top.location.href = searchUrl;
            return true;
        }


        function CustomAdvSearch() {

            var key = document.getElementById("customInputKeywords").value;
            var searchUrl = "/Search/advanced.aspx?k=" + key;
            top.location.href = searchUrl;
            return true;
       
        }
    </script>


<table class="ms-sbtable ms-sbtable-ex" border="0">
            <tbody>
                <tr class="ms-sbrow">
                    <td class="ms-sbcell">
                        <input type="text"
                            style=" 280px" id="customInputKeywords"
                            class="ms-sbplain"
                            title="输入搜索文字"
                            alt="输入搜索文字" maxlength="200"/>
                    </td>
                    <td class="ms-sbgo ms-sbcell">
                        <a id="customGo" title="开始搜索"
                            href="javascript:CustomSearch()">
                            <img style="border-right- 0px; border-top- 0px; border-bottom- 0px;
                                border-left- 0px"
                                title="开始搜索" alt="开始搜索"
                                src="/_layouts/images/gosearch.gif"/></a>
                    </td>
                    <td class="ms-sbcell ms-sblink">
                        <a id="customAdvSearchLink" title="高级搜索" href="javascript:CustomAdvSearch()">高级搜索</a>
                    </td>
                </tr>
            </tbody>
        </table>

原文地址:https://www.cnblogs.com/dbasys/p/2127527.html