jQuery:如何AJAXify WordPress搜索?

时间:2011-07-13 10:59:14

标签: jquery ajax wordpress search

我正在尝试将AJAX功能添加到基于WordPress的站点。

当访问者点击菜单链接时,此代码会将所需页面的文本文本加载到主页面中:

$('#access a').click(function(event) {
    event.preventDefault();
    var url = $(this).attr('href');
$('#content').fadeOut(500, function() {
    $('#content').load(url, function() {
        $('#content').fadeIn(500);
        }); 
    });
});

我的问题在于搜索表单。它在页面中具有以下结构:

<div id="my_search"><form role="search" method="get" id="searchform" action="http://myurl.com/" >
    <input type="text" value="" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="Search" />
    </div>
    </form></div>

当我在表单中键入单词测试,然后单击键盘上的#searchsubmit“按钮或Enter键时,我被重定向到一个新的WordPress页面,其中包含以下地址:

http://www.myurl.com/?s=testing

这是使其有效的PHP代码:

<?php if ( have_posts() ) : ?>
                <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyten' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                <?php
                /* Run the loop for the search to output the results.
                 * If you want to overload this in a child theme then include a file
                 * called loop-search.php and that will be used instead.
                 */
                 get_template_part( 'loop', 'search' );
                ?>
<?php else : ?>
                <div id="post-0" class="post no-results not-found">
                    <h2 class="entry-title"><?php _e( 'Nothing Found', 'twentyten' ); ?></h2>
                    <div class="entry-content">
                        <p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentyten' ); ?></p>
                    </div><!-- .entry-content -->
                </div><!-- #post-0 -->
<?php endif; ?>

问题:我想阻止#searchsubmit上的默认行为点击并按Enter键点击,然后将搜索结果的内容(http://www.myurl.com/?s=whateverwordItype)加载到主页面的#content。我怎么能这样做?

(我想我只需要以某种方式在jQuery代码中插入php代码,但是这个任务对我的jQuery和PHP有点了解。)

如果可能的话,我会感谢知识渊博的人提供的代码片段。

2 个答案:

答案 0 :(得分:5)

使用一些简单的jQuery和javascript

要通过ajax请求加载搜索结果,您可以使用以下代码直接从搜索结果页面获取搜索结果:

// Bind the submit event for your form
$('#searchform').submit(function( e ){ 

    // Stop the form from submitting
    e.preventDefault();

    // Get the search term
    var term = $('#s').val();

    // Make sure the user searched for something
    if ( term ){

        $.get( '/', { s: term }, function( data ){

            // Place the fetched results inside the #content element
            $('#content').html( $(data).find('#content') );

        });

    }

});

注意:$(data).find('#content')假设您的search.php结果汇总在ID为内容的元素中,即<div id="#content"> ... Results ... </div>

以上内容将放入外部文件或放在结束</body>标记之前。

您可以自定义search.php页面以整理输出结果。

答案 1 :(得分:2)

试试这个

$("#searchsubmit").click(function(e){
    e.preventDefault();
    var search_val=$("#s").val(); 
    $.post('search.php',{search_string:search_val},function(data){
        if(data.length>0){
            $("#results").html(data);
        }
      });
});

这会调用search.php,它会将搜索字符串作为$_POST['search_string']之类的帖子变量,在这里,您处理搜索字符串并回显结果,这将返回到页面{ {1}}。拥有ID为data的div,results会将search.php中的结果放入div中。如果你想使用get,那么只需将jQuery funstion中的post替换为get,你就可以在search.php中使用$("#results").html(data);

希望这会对你有所帮助

相关问题