嘿,我在 body 标签中定义了一个像这样的 php 变量:
<?php $v=null;?>
现在我使用此代码来检查变量是否通过 url 设置
if ("<?php echo $_GET["v"];?>;" == null) {
// Do something because the variable is set through the url
}
else {
// Do something else because the variable is not set through the url
}
我使用的网址如下:www.example.com/index.php?v=12345
当我通过使用此网址打开索引来运行此代码时,它说 v 未定义.. 当我通过使用“www.example.com/index.php”打开索引来运行此代码时,它也是未定义的..但不是“null”
这里有什么问题吗?谢谢
最佳答案
尝试一下
var my_var_arr = location.search.replace('?', '').split('=');
if (my_var_arr[1] == null) {
// Do something because the variable is set through the url
}
else {
// Do something else because the variable is not set through the url
}
试试这个 FIDDLE
关于javascript - 获取 javascript 中 url 设置的 php 变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20437980/