1,用jquery获取url里的参数这里有用到正则,这里的q的值就是我们想要的url里的参数
(function ($) {
$.getUrlParam =function (q) {
var search =document.location.search;
//alert(search);
var pattern =new RegExp("[?&]" + q +"\=([^&]+)","g");
var matcher =pattern.exec(search);
var items =null;
if (null !=matcher) {
try {
items =decodeURIComponent(decodeURIComponent(matcher[1]));
}catch (e) {
try {
items =decodeURIComponent(matcher[1]);
}catch (e) {
items =matcher[1];
}
}
}
return items;
}
})(jQuery);
2,然后是获取q里的参数
var searchGoods =jQuery.getUrlParam('q');
console.log(searchGoods ) 就是我们想要的值
ps:在项目中先要做的效果是点击搜索栏,如果url里的q值与搜索框里的值相等,则清楚input框里的值,不再搜索(因为在搜索的过程中把搜索值带到了搜索页且搜索框是隐藏的,为了避免重复搜索)有如下操作
1, 目的是为了匹配input里的格式
if(searchGoods !==null){
var transferGoods =searchGoods.indexOf('+');
if(transferGoods >0){
searchGoods =searchGoods.replace(/\+/g,' ')
}
}
,
jQuery(function(){
(function ($) {
$.getUrlParam =function (q) {
var search =document.location.search;
//alert(search);
var pattern =new RegExp("[?&]" + q +"\=([^&]+)","g");
var matcher =pattern.exec(search);
var items =null;
if (null !=matcher) {
try {
items =decodeURIComponent(decodeURIComponent(matcher[1]));
}catch (e) {
try {
items =decodeURIComponent(matcher[1]);
}catch (e) {
items =matcher[1];
}
}
}
return items;
}
})(jQuery);
var searchGoods =jQuery.getUrlParam('q');
if(searchGoods !==null){
var transferGoods =searchGoods.indexOf('+');
if(transferGoods >0){
searchGoods =searchGoods.replace(/\+/g,' ')
}
}
jQuery('.header-container.type20 .header .form-search button.button').click(function() {
if(searchGoods ==jQuery('.header-container.type20 .header .form-search #search').val()){
jQuery('.header-container.type20 .header .form-search #search').val('');
};
setTimeout(function () {
jQuery('.header-container.type20 .header .form-search #search').focus();
},0);
jQuery('.header-container.type20 .header .form-search #search').css('display','block');
});
jQuery('.header-container.type20 .header .form-search #search').focus(function() {
if (jQuery('.wrapper-hotSearch').is(':hidden') ) {
jQuery('.wrapper-hotSearch').show();
}
});
jQuery(document).bind("click",function(e){
var target =jQuery(e.target);
if(!target.is('.header-container.type20 .header .form-search #search') && !target.is('.header-container.type20 .header .form-search button.button')) {
if (jQuery('.wrapper-hotSearch').is(':visible') ) {
jQuery('.wrapper-hotSearch').hide();
}
}
});
})