search320.js 1.05 KB
/*
 * @description: 搜索
 * @author: chenglong.wang@yoho.cn
 * @date: 2015/1/13
 */

var $ = require("jquery");

        require("jquery.autocomplete");

exports.init = function () {
    var search = $('.search_input input');
    var searchBtn = $('.search_btn');
    search.keydown(function (event) {
        if (event.keyCode == 13) {
            searchBtn.trigger("click");
        }
    }).autocomplete('/search/default/words', {
        width: 290,
        scrollHeight: 100,
        max: 10,
        selectFirst: false,
        autoFill: false,
        formatResult: function (row, data) {
            return data.replace(/<\/?[^>]*>/g, '');
        }
    }).result(function (event, data, formatted) {
        window.location.href = './search?q=' + encodeURIComponent(formatted.replace(/<\/?[^>]*>/g, ''));
    });

    searchBtn.click(function () {
        if (search.val() !== "") {
            window.location = YohoConfig.mainUrl + "/search?q=" + search.val();
            return false;
        } else {
            $(document).trigger("click");
        }
    });
};