help-search.js 1.68 KB
/**
 * [帮助中心 搜索]
 * @author: wsl(shuiling.wang@yoho.cn)
 * @date: 2016/12/02
 */
var helpSearch = {
    $search: $('.help-search'),
    init: function() {
        var _this = this;
        var $helpSearchForm = $('#help-search-form');
        var $searchBtn = $('.search-btn');

        _this.searchDefaultVal();

        $searchBtn.click(function() {
            if (_this.$search.val() === '请输入您想知道的帮助信息') {
                _this.$search.val('');
            }
            $helpSearchForm.submit();
        });

        _this.$search.keyup(function(e) {
            var key = $(this).val();

            if (key === '请输入您想知道的帮助信息') {
                $(this).val('');
            }

            if (e.which === 13) {
                if (key) {
                    $helpSearchForm.submit();
                }
            }
        });
    },
    searchDefaultVal: function() {
        var _this = this;
        var defaultVal = '请输入您想知道的帮助信息';
        var startVal = defaultVal;
        var defaultColor = '#989898';

        if (_this.$search.val()) {
            startVal = _this.$search.val();
            defaultColor = '#1b1b1b';
        }

        _this.$search.focus(function() {
            var key = _this.$search.val();

            if (key === defaultVal) {
                _this.$search.val('').css('color', '#1b1b1b');
            }
        }).blur(function() {
            var key = _this.$search.val();

            if ($.trim(key) === '') {
                _this.$search.val(defaultVal).css('color', '#989898');
            }
        }).val(startVal).css('color', defaultColor);
    }
};

module.exports = helpSearch;