search.js 1.07 KB
/**
 * 搜索JS
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/19
 */

var $ = require('yoho.zepto');

var $input = $('#search-input > input');

var $clear = $('#search-input > .clear-input');

var $history = $('.history');

$('#clear-history').bind('touchstart', function() {
    $.ajax({
        type: 'POST',
        url: '/search/clearHistory',
        success: function(data) {
            if (data.code === 200) {
                $history.html(''); //clear search history items
            }
        }
    });
});

$('#search').bind('touchstart', function() {
    var val = $input.val();

    $.ajax({
        type: 'POST',
        url: '/search',
        data: {
            val: val
        },
        success: function(data) {
            if (data.code === 200) {
                location.href = data.data;
            }
        }
    });
});

$input.bind('input', function() {
    if ($input.val() === '') {
        $clear.addClass('hide');
    } else {
        $clear.removeClass('hide');
    }
});

$clear.bind('touchstart', function() {
    $input.val('').trigger('input');
});