2009-12-18 74 views
1

基本上,如果我在:http://example.com/content/connect/152,我想知道url中是否存在「connect」,然後將菜單的選定值設置爲具體的事情......(url也可以像http://example.com/content/connections,在這種情況下,它仍然應該匹配...)jquery - 嘗試使用與window.location.pathname的模式匹配

這是我一直很努力,這,顯然是不工作... 。

var path = window.location.pathname; 
if(path).match(/^connect) { 
$("#myselect").val('9'); 
} else { 
$("#myselect").val('0'); 
} 
+0

我想你只是在if中有一個錯字。不應該是如果(path.match(/^connect))? – 2009-12-18 19:24:31

回答

4

由於連接可以在你的網址在任何地方也沒有必要添加^

嘗試:

if (path.match("/connect")) 

這個假設您希望一個「/」在連接之前

+0

謝謝... (和其他人!) – n00b0101 2009-12-18 19:33:36

0

您的正則表達式只會匹配以連接開頭的值。

你可能想這樣的:

if(path.match(/^.*connect.*$/)) { 
+0

再一次,我在OP的評論中描述了同樣的錯字。 (path.match(/^.* connect。* $ /)){... – 2009-12-18 19:30:52

+0

if(path.match(/^.* connect。* $ /)){ – ryanulit 2009-12-18 19:30:52