[英]How do I pass a javascript variable in a get statement?
I want to pass the variable testVar into the get statement so I can compare the value of it with the PHP variable param.我想将变量 testVar 传递到 get 语句中,这样我就可以将它的值与 PHP 变量参数进行比较。 When I log testVar in the console, I get undefined.
当我在控制台中记录 testVar 时,我得到了未定义。 I am very new to programming so any help would be appreciated.
我对编程非常陌生,因此将不胜感激。
var testVar= "hello"
;
$.get('schools.php', function (param) {
console.log(testVar) //This gives me 'undefined'
if (param[1]== testVar){
console.log("it is working!")
}
})
This is a scope problem.这是一个 scope 问题。 The variable inside the function is different from the global, so, therefore, it gets reassigned inside your function.
function 中的变量与全局变量不同,因此,它会在 function 中重新分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.