userCommonSearch.js
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Created by moli on 15/5/26.
*/
$(function(){
var DiliPath = $.extend(DiliPath,
{
loginPath : "https://passport.nong12.com/uc/login.aspx",
registPath : "https://passport.nong12.com/regedit/regedit.aspx",
homePath : "http://search.nong12.com/",
logoOut : "https://passport.nong12.com/uc/loginService?ltype=logout",
userInfo : "http://user.nong12.com/account/security.html"
}
);
var MethodSearch = {
searchval : {type : "STG"},
/* 依据data.dtype 发送ajax请求 */
http : function(url, success, error, data){
var parm = {
type : 'GET',
url : url,
dataType : "jsonp",
cache : false,
success : function(data){
success(data);
},
error : function(ero){
if(error){error(ero)};
}
};
if(data && data.dtype){
parm.jsonp = data.dtype;
}
if(data && data.call){
parm.jsonpCallback = data.call;
}
$.ajax(parm);
},
/* 初始化 需要使用的函数*/
init : function(){
this.intSearch();
},
intSearch : function(){//初始化搜索
var keywords = ["搜“黄瓜”试试,大黄瓜小黄瓜应有尽有", "搜“四川”试试,保证多快好省"];
$(".search-input input").attr("placeholder", keywords[(Math.ceil(Math.random() * 2) - 1)]);
this.changeSearch();
this.newAutocomplete();
},
changeSearch : function(){//搜索类型切换
var view = $(".search-type li");
view.click(function(){
view.eq(0).text($(this).text()).append('<b class="icon-direction icon-direction-middle"></b>');
if($(this).text() == "产品"){
view.eq(1).text("店铺");
MethodSearch.searchval.type = "STG";
}else{
view.eq(1).text("产品");
MethodSearch.searchval.type = "STORE";
}
});
},
newAutocomplete : function(){//创建搜索
$('#autocomplete').autocomplete({
serviceUrl: DiliPath.homePath + "newTip.html",
paramName: "keywords",
dataType: "jsonp",
jsonp: "callFn",
appendTo: ".search-input",
triggerSelectOnValidInput: false,
extraParams : function(){
var self = this;
self.params['type'] = MethodSearch.searchval.type;
},
onSelect: function (suggestion) {
MethodSearch.searchSend();
}
});
$('#autocomplete').keypress(function (event) {
if (event.which == 13) {
MethodSearch.searchSend();
}
});
$(".searchForm").click(function () {
MethodSearch.searchSend();
});
},
searchSend : function(){//搜索结果页面
var keyword = $("#autocomplete").val();
keyword = $.trim(keyword);
keyword = encodeURIComponent(keyword);
if (jQuery.trim(keyword) == "") {
var def = $("#autocomplete").attr("placeholder");
keyword = def.substring(2, def.indexOf("”"));
}
var searchtype = MethodSearch.searchval.type == "STG" ? "goods" : "shop";
window.location.href = DiliPath.homePath + searchtype + ".html"
+ "?key=" + keyword;
}
};
MethodSearch.init();
});