jquery - 尝试使用与 window.location.pathname 的模式匹配

n00*_*101 1 jquery select path match

基本上,如果我在:http://example.com/content/connect/152,我想知道 url 中是否存在“connect”,然后将菜单的选定值设置为特定的值.. . (该 url 也可能类似于http://example.com/content/connections,在这种情况下,它仍然应该匹配...)

这就是我一直在尝试的,这显然不起作用......

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

Pat*_*ick 5

由于连接可以在您的 URL 中的任何位置,因此无需添加 ^

尝试 :

if (path.match("/connect"))
Run Code Online (Sandbox Code Playgroud)

这假设您在连接之前想要一个“/”