select下拉框选中项改变时,通过操作查询字符串参数,实现页面跳转,这种需求如果不要求用ajax去实现的话,用处就很大了,主要是操作url的查询字符串参数来实现,中间还会涉及到location、replace()以及正则表达式的一些应用,源码如下:
第1页数据
第2页数据
第3页数据
$("#select").on("change",function(){
var search = location.search;
var temp = "order_page="+this.value;
//当前查询字符串不包含要传递的参数order_page时则进行字符串叠加
if (search.indexOf("order_page") == -1) {
location.search = search + temp;
} else if (!eval("/" + temp + "/").test(search)) { //此判断条件,防止当前地址已包含传递的查询字符串时还发生页面刷新
//当前查询字符串包含要传递的参数order_page时则进行参数order_page值的替换
location.search = search.replace(/order_page=\d{1,}/, temp);
}
})
var search = location.search;
if (search.indexOf("order_page") != -1) {
var number=search.match(/(?:order_page=)(\d){1,}/)[1];
$("#select option")[number-1].selected="selected";
// $("#select option").eq(number-1).attr("selected","selected");
}
提示:你可以先修改部分代码再运行。
做这个功能只是练手,单纯是为了熟悉下js相关的一些操作。select标签切换,加载符合选中项条件的数据内容,还是推荐使用ajax局部刷新的技术来实现,这样用户能更快的看到自己想要看到的数据,局部刷新对提高用户体验还是有一定帮助的。