简体   繁体   English

如何在 get 语句中传递 javascript 变量?

[英]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.

相关问题 如何将 php/html 中的程序名称作为变量传递给 javascript,而不是在“$.get”语句中对其进行硬编码 - How do I pass a program name from php/html, as a variable to javascript rather than hard coding it in the ' $.get' statement 如何将JS变量传递到INPUT语句的“ value =”参数中 - How do I pass a JS variable into the “value=” parameter of an INPUT statement 如何在 for 语句中将 django 变量传递给 javascript? - How do I pass django variables to javascript in a for statement? 如何使用javascript将值传递到sql语句中? - How do I pass in a value into a sql statement using javascript? 如何将JavaScript确认变量传递给php变量 - how do i pass JavaScript confirm variable to php variable 如何将字符串变量传递给javascript函数并使用它获取元素 - How do i pass a string variable to a javascript function and use it to get element 如何在Javascript的if语句中检查数字和运算符的变量 - How do I check a variable for a number and an operator in an if statement in Javascript 如何使用JavaScript在PHP GET变量上运行if语句? - How can I run an if statement on a PHP GET variable using JavaScript? 如何通过网址传递JavaScript变量? - How do I pass a javascript variable through a URL? 如何在 JavaScript 中的 POST 请求 URL 中传递变量? - How do I pass a variable inside a POST request URL in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM