I can use some advise on ajaxlivesearch.com function in jquery

时间:2016-04-04 17:08:49

标签: php jquery mysql ajax livesearch

I have a searchbar included with the livesearch.com functionality from ajaxlivesearch.com

The problem is when i click on a result nothing happens. I want to send a query and show a result page when i click on the result. Before i included the ajaxlivesearch funtion i just typed in a keyword and pushed enter and then my result page showed up, this happens with an query. But after i included the ajaxlivesearch funtion pressing enter was not an option anymore, nothing happens.

I think the problem is within these jQuery lines;

jQuery(".mySearch").ajaxlivesearch({
    loaded_at: <?php echo $time; ?>,
    token: <?php echo "'" . $token . "'"; ?>,
    maxInput: <?php echo $maxInputLength; ?>,
    onResultClick: function(e, data) {
        // get the index 1 (second column) value
        var selectedOne = jQuery(data.selected).find('td').eq('1').text();

        // set the input value
        jQuery('.mySearch').val(selectedOne);

        // hide the result
        jQuery(".mySearch").trigger('ajaxlivesearch:hide_result');
    },
    onResultEnter: function(e, data) {
        // do whatever you want
        // jQuery(".mySearch").trigger('ajaxlivesearch:search', {query: 'test'});
    },
    onAjaxComplete: function(e, data) {
        // do whatever you want
    }
});

I hope someone can help me out

Cheers!

1 个答案:

答案 0 :(得分:0)

在您做任何事情之前,请转到ajaxlivesearch.js文件,找到这行代码 - &gt;

// disable the form submit on pressing enter
        form.submit(function () {
            return false;
        });

当你按下回车键时,基本上它会禁用任何形式的事情,这与你想要做的事情正好相反。显然你需要改变它,例如

$("#search_ls_query").submit(function(){
            return true;
        });

现在,(这不是一个完全重要的补充,但是,在您添加到索引页面的“onResultEnter”区域中提交表单。例如。

jQuery(document).ready(function(){
jQuery(".mySearch").ajaxlivesearch({
    loaded_at: <?php echo $time; ?>,
    token: <?php echo "'" . $token . "'"; ?>,
    maxInput: <?php echo $maxInputLength; ?>,
    onResultClick: function(e, data) {
        // get the index 0 (1st column) value
        var selectedOne = jQuery(data.selected).find('td').eq('0').text();

        // set the input value
        jQuery('.mySearch').val(selectedOne);

        // hide the result
        jQuery(".mySearch").trigger('ajaxlivesearch:hide_result');
    },

    onResultEnter: function(e, data) {
        $("#search_ls_query").submit();

    },
    /*  #search_ls_query is the id of the default form that is submitted when you press enter (Find in ajaxlivesearch.js)*/

    onAjaxComplete: function(e, data) {
    }
});

})

现在转到ajaxlivesearch.js文件并找到:

var wrapper = '<div class="' + ls.container_class + '">' +
                '<form accept-charset="UTF-8" class="' + ls.form_class + '" id="' + ls.form_class + '_' + elem_id + '" name="ls_form">' +
                '</form>' +
                '</div>';

并在此表单中添加操作,因为这是在搜索栏中按Enter键时激活的操作。所以说吧:

var wrapper = '<div class="' + ls.container_class + '">' +
                '<form accept-charset="UTF-8" action="search.php" class="' + ls.form_class + '" id="' + ls.form_class + '_' + elem_id + '" name="ls_form">' +
                '</form>' +
                '</div>';

这样它实际上有一个发送值的目的地。因此,发送的值的名称是“ls_query”,因此无论您在条形图中键入的内容现在都将插入到“ls_query”的值中。因此,一旦您按Enter键并到达新页面,您就可以使用此变量来过滤搜索选项,或者您需要的任何内容。例如,您可以在搜索页面上写下:

$ls_query = secure($_GET['ls_query'],$mysqli);      

    $sql = "SELECT *
    FROM users
    WHERE username   ='$ls_query'";

我花了一段时间才弄明白。但如果你还在寻找答案,这对我有用。让我知道它是怎么回事!