您的页面更改,因此脚本再次加载。您需要测试您拥有的网址
更新以处理不同的 orderby 值
$(document).ready(function() {
$('.orderby').click(
function(){
var srch = location.search;
if (srch) srch = srch.substring(1); // lose the ?
var val = $(this).val();
if (srch.indexOf(val+"ASC")!=-1)
srch = srch.replace(val+"ASC",val+"DESC");
else if (srch.indexOf(val+"DESC")!=-1)
srch = srch.replace(val+"DESC",val+"ASC");
else if (!srch || srch.indexOf("orderby")!=-1) {
// if srch was empty or contained another value than this'
srch ='orderby='+$(this).val()+'ASC';
}
var loc = (location.search)?location.href.split("?")[0]:location.href;
window.location = loc + "?"+srch;
});
});
PS:document.location.href 在很多年前就被弃用了。请改用 document.URL 或 window.location.href
PPS:正如 MilkyWayJoe 正确指出的那样 - 为什么要麻烦 - 如果页面从服务器加载到当前页面而不是 iframe 或 ajaxed,然后在服务器上设置按钮的值并有一个正常的链接 - 这样就可以了即使关闭 JS 也可以工作
更新2
如果你使用 AJAX,你想做一些事情,比如
$(document).ready(function() {
if (location.hash.indexOf(....) {
// find the element which is mentioned in the orderby and click it
var $elem = ....
$elem.click(); // or trigger
}
$('.orderby').toggle(
function(){
$("#someLabel").html("DESC");
var orderBy = $(this).val()+'ASC';
location.hash = orderBy; // now URL will be ...#nameASC
$("#content").load('soneserverprocess?orderby='+orderBy);
},
function(){
$("#someLabel").html("ASC");
var orderBy = $(this).val()+'DESC';
location.hash = orderBy; // now URL will be ...#nameDESC
$("#content").load('someserverprocess?orderby='+orderBy);
});
});
如果要使用浏览器内排序,请使用其中之一
http://www.tripwiremagazine.com/2012/02/jquery-filter-sort-plugins.html
并且有
$(document).ready(function() {
if (location.hash.indexOf(....) {
// find the element which is mentioned in the orderby and click it
var $elem = ....
$elem.click(); // or trigger
}
$('.orderby').toggle(
function(){
$("#someLabel").html("DESC");
var orderBy = $(this).val()+'ASC';
location.hash = orderBy; // now URL will be ...#nameASC
$("#content").sort(....); // depending on plugin
},
function(){
$("#someLabel").html("ASC");
var orderBy = $(this).val()+'DESC';
location.hash = orderBy; // now URL will be ...#nameDESC
$("#content").sort(....); // depending on plugin
});
});