2012-02-10 40 views
0

我試圖從url中讀取查詢字符串,並根據查詢字符串提醒消息。字符串正在完美傳遞,我只是在閱讀頁面時遇到麻煩。我究竟做錯了什麼?jquery從url中獲取查詢字符串

可能的查詢字符串

.../playlist.html?vid=1 
.../playlist.html?vid=2 

JS

$(function() { 
if (window.locaion.search.indexOf('vid=1') > -1) { 
     alert('1'); 
} else if (window.locaion.search.indexOf('vid=2') > -1) { 
     alert('2'); 
} 
}); 
+0

locaion是一個錯字嗎? – 2012-02-10 22:46:15

+0

你已經寫了'locaion'而不是'location'。在你的運行代碼中是否有錯字,或者只是在這裏的問題? – Rophuine 2012-02-10 22:46:49

+0

錯字?嘗試位置,而不是你有什麼。 – 2012-02-10 22:46:54

回答

0

兩個問題:輸入了錯誤的位置,你需要的toString(),因爲位置ISN」一個字符串。所以更正後的版本:

$(function() { 
    if (window.location.toString().indexOf('vid=1') > -1) { 
     alert('1'); 
    } else if (window.location.toString().indexOf('vid=2') > -1) { 
     alert('2'); 
    } 
}); 
+0

...我試圖糾正位置錯字,它仍然不會爲我工作。將.search更改爲.toString() - 但也許.search是一個jQuery的東西,我沒有加載。 – Rophuine 2012-02-10 22:53:34

0

變化window.locaionwindow.location