如何检查网址是否包含多个字符串. Javascript/jQuery [英] how to check if the url contains multiple strings. Javascript/jquery

查看:107
本文介绍了如何检查网址是否包含多个字符串. Javascript/jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在问题中如何检查网址是否包含给定的字符串. Javascript/jquery 答案检查网址是否包含给定的字符串.我想扩展它以检查它是否包含两件事name = franky&&性别=男性.我尝试过

In the question how to check if the url contains a given string. Javascript/jquery the answer checks if the url contains a given string. I want to extend this to check if it contains two things name=franky && gender=male. I tried

if(window.location.href.indexOf("name=franky" && "gender=male") > -1)

最初似乎有效,但是经过进一步测试,我发现如果仅使用姓名或性别之一,它似乎可以工作.我在哪里错了?

which appeared to work initially, but with further testing I have found it seems to work if only one of name or gender is present. Where am I going wrong here?

推荐答案

您可以将其分为两部分并调用indexOf两次,而不是尝试在同一调用中检查两个字符串.像这样:

You could break it into two parts and call indexOf twice instead of trying to check for both strings in the same call. Something like this:

if(window.location.href.indexOf("name=franky") > -1 && 
    window.location.href.indexOf("gender=male") > -1)

这篇关于如何检查网址是否包含多个字符串. Javascript/jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆