2015-08-28 39 views
0

我編碼分頁與JS分頁的JavaScript出了錯

出這樣

<a id="prev">Previous Page</a> 
<a id="next">Next Pages</a> 

和JS代碼這樣

$('#next').click(function(){ 
    var url = window.location.href; 
    var urllen = url.length; 
    var cur = parseInt((url.substr(urllen-1)).substr(0,1)); 
    var nurl = url.substr(0,(urllen-1))+(cur+1); 
    if(cur=="NaN") { window.location = (url); } 
    else { window.location = (nurl); } 
}); 
$('#prev').click(function(){ 
    var url = window.location.href; 
    var urllen = url.length; 
    var cur = (url.substr(urllen-1)).substr(0,1); 
    if(cur==1||cur=="NaN") { window.location = (url); } 
    else { var nurl = url.substr(0,(urllen-1))+(cur-1); window.location = (nurl); } 
}); 

和我的網址類似

http://localtest/rftpages/record.html?s=1&l=1&bike_id=1 

讓我來解釋一下reas在我使用的JavaScript方法是我不想改變我的網址,其中包含頁面變量,我在整個頁面中使用

所以我在做的是獲取所有的URL並將bike_id值更改爲下一個/上

,問題是,當數到19或URL類似

http://localtest/rftpages/record.html?s=1&l=1&bike_id=19 

然後我去旁邊再網址將成爲

http://localtest/rftpages/record.html?s=1&l=1&bike_id=110 

任何想法/建議修復 這個 ?

感謝

回答

2

你應該做的是從它的基礎上點擊一下查詢字符串和遞增或遞減搶頁面。

所有你需要的是這個函數來獲取參數:

function getParameterByName(name) { 
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); 
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
     results = regex.exec(location.search); 
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

所以,如果我假設你的例子:

http://localtest/rftpages/record.html?s=1&l=1&bike_id=19 

然後你就可以改變你的功能是:

$('#next').on("click", function() { 
    var currentPageParameter = getParameterByName("bike_id"); 
    var s = getParameterByName("s"); 
    var l = getParameterByName("l"); 
    var myPage = parseInt(currentPageParameter); 
    if (! isNaN(myPage)) { 
      myPage = myPage + 1; 
      window.location = location.protocol + '//' + location.host + location.pathname + "?s=" + s + "&l=" + l + "&bike_id=" + myPage; 
     } 
}); 
$('#prev').on("click", function() { 
var currentPageParameter = getParameterByName("bike_id"); 
    var s = getParameterByName("s"); 
    var l = getParameterByName("l"); 
    var myPage = parseInt(currentPageParameter); 
    if (! isNaN(myPage)) { 
      myPage = myPage - 1; 
      window.location = location.protocol + '//' + location.host + location.pathname + "?s=" + s + "&l=" + l + "&bike_id=" + myPage; 
     } 
}); 
+0

好的,現在工作正常,謝謝 –