2012-03-27 60 views
0

我有兩個htmls,即index.html和grid.html。我需要根據在index.html文件中選擇的下拉值來刷新網格內容。如何從jquery加載函數中檢索url參數?

在我的index.html頁面我有這個代碼,

$(document).ready(function() { 
    $('#orderTypeOptions').change(function() { 
     var oType = $('#oTypeOptions').val(); 
     $('#ordersGrid').load('grid2.html?oType='+oType); 
    }); 
}); 

我的問題是,我怎麼能檢索網格頁面的URL參數「oType」值?誰能幫忙?

+0

如何渲染HTML樣子?服務器的響應是什麼? – gdoron 2012-03-27 14:33:22

+1

如果您只是在瀏覽器欄中輸入網址,這與您將如何檢索該值無關。我不認爲這是'javascript'或'jquery'問題。 – Curt 2012-03-27 14:35:06

+0

我得到正常數據的網格。 (根據我們在下拉列表中更改的過濾器類型不會進行渲染)。此外,我無法檢索grid.html頁面中的「oType」參數值。 – 2012-03-27 14:35:32

回答

2

它作爲字符串在window.location.search變量中。您需要解析它以命名值對並找到您感興趣的變量。

+0

我在哪裏需要這樣做?在index.html(或)grid.html中 – 2012-03-27 14:48:57

+0

在網格html中。 – 2012-03-27 14:53:14

+0

不幸的是,它不工作。 – 2012-04-01 13:17:11

0

window.location.search

+0

我需要這樣做嗎?在index.html(或)grid.html – 2012-03-27 14:49:11

0

使用JavaScript函數來檢索URL參數。

例採取摘自:Get escaped URL parameter

function getURLParameter(name) { 
    return decodeURI(
     (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] 
    ); 
} 

用法示例在grid.html文件:

var oType = getURLParameter("oType");

+0

當我使用任何自定義的JavaScript URL提取函數,我只能得到絕對URL(如localhost:8080/webservice/grid.html),它不返回任何網址參數 – 2012-03-27 14:47:17

+0

@DavidR你說'alert(location.search);'只顯示沒有查詢字符串的URL? – 2012-03-27 14:54:26