jquery - 尝试使用与window.location.pathname匹配的模式
基本上,如果我位于: 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');
}
Basically, if I'm at: http://example.com/content/connect/152, I want to find out if "connect" is present in the url and then set the selected value of a menu to something specific... (The url could also be something like http://example.com/content/connections, in which case, it should still match...)
This is what I've been trying, which, clearly isn't working....
var path = window.location.pathname;
if(path).match(/^connect) {
$("#myselect").val('9');
} else {
$("#myselect").val('0');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的正则表达式将仅匹配以 connect 开头的值。
你可能想要这个:
Your regex will only match values beginning with connect.
You probably want this:
由于 connect 可以位于 URL 中的任何位置,因此无需添加 ^
try :
这假设您需要在连接之前添加一个“/”
Since connect can be anywhere in your URL there is no need to add the ^
try :
this assume that you want a "/" right before a connect