如何在ajax代码中访问javascript变量值

时间:2022-08-27 17:42:33

I am trying to access the javascript variable value from this code to ajax code

我试图从这段代码访问javascript变量值到ajax代码

I tried to access like this but it is not working-

我试图像这样访问但它不工作 -

javascript code

<script type="text/javascript">

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
       var a = radios[i].value
            break;
        }
    }
}


function remove(a) 
{
     alert(a);
     if(a=="") 
     {
         document.getElementById("txtHint").innerHTML="";
         return;
     }

     if(window.XMLHttpRequest) 
     {
         xmlhttp=new XMLHttpRequest();
     } 
     else 
     {
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange = function() 
     {
         if(xmlhttp.readyState==4 && xmlhttp.status==200) 
         {
              document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
         }
     }
     xmlhttp.open("GET","theaterdel.php?q="+a,true);
     xmlhttp.send();
}

</script>

In this code I am trying to access the variable a's value from getResults() function to remove() like this

在这段代码中,我试图从getResults()函数中访问变量a的值来删除()

remove(a)
{}

But it is not working, when I alert a's value inside remove(a){}, it is printing undefined

但它不起作用,当我在remove(a){}内警告一个值时,它是打印未定义的

5 个解决方案

#1


4  

Globally define the variable -

全局定义变量 -

var a= "hi";

function getResults(){ ... }

function remove() { alert(a); ... }

#2


4  

try this

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
            var a = radios[i].value
            remove(a);
            break;
        }
    }
}

call a function inside

在里面调用一个函数

hope it will help.

希望它会有所帮助。

#3


3  

You have declared variable "a" as local to getresults function. Declare variable "a" as global, don't use var keyword.

您已将变量“a”声明为getresults函数的本地变量。将变量“a”声明为全局变量,不要使用var关键字。

if (radios[i].checked) 
    {
     a = radios[i].value
        break;
    }

You are having two different functions and to get variable of one in another function, declare it globally.

你有两个不同的函数,并在另一个函数中获得一个变量,全局声明它。

#4


2  

you need to declare the variable outside the function like

你需要在函数之外声明变量

var a = null
function getResults()

when you declare it outside the function you can access the variable from anywhere you want

当你在函数外声明它时,你可以从任何你想要的地方访问变量

also, look into jQuery .ajax() for simpler code regarding AJAX

另外,请查看jQuery .ajax()以获取有关AJAX的更简单代码

#5


2  

You've defined a inside the getResults() function using the 'var' keyword. This means it will only be in scope inside that function.

您已使用'var'关键字在getResults()函数内定义了一个。这意味着它只会在该函数内的范围内。

If you want to use that variable in another function, you'll need to pass it across when you call the other function - simple example is below:

如果你想在另一个函数中使用那个变量,你需要在调用另一个函数时传递它 - 简单的例子如下:

function myFunction() {
 var a = "this is a";
 secondFunction(a);
}

function secondFunction(a) {
 alert(a);
}

An alternative is to declare the variable so it has global scope - but that's not a great idea unless you're confident that it won't conflict with any other global variables that might exist on your page.

另一种方法是声明变量,使其具有全局范围 - 但这不是一个好主意,除非您确信它不会与页面上可能存在的任何其他全局变量冲突。

You're better off constraining scope unless you absolutely need to have global variables. It'll make your code neater, and debugging easier.

除非绝对需要全局变量,否则最好限制范围。它会使您的代码更整洁,调试更容易。

#1


4  

Globally define the variable -

全局定义变量 -

var a= "hi";

function getResults(){ ... }

function remove() { alert(a); ... }

#2


4  

try this

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
            var a = radios[i].value
            remove(a);
            break;
        }
    }
}

call a function inside

在里面调用一个函数

hope it will help.

希望它会有所帮助。

#3


3  

You have declared variable "a" as local to getresults function. Declare variable "a" as global, don't use var keyword.

您已将变量“a”声明为getresults函数的本地变量。将变量“a”声明为全局变量,不要使用var关键字。

if (radios[i].checked) 
    {
     a = radios[i].value
        break;
    }

You are having two different functions and to get variable of one in another function, declare it globally.

你有两个不同的函数,并在另一个函数中获得一个变量,全局声明它。

#4


2  

you need to declare the variable outside the function like

你需要在函数之外声明变量

var a = null
function getResults()

when you declare it outside the function you can access the variable from anywhere you want

当你在函数外声明它时,你可以从任何你想要的地方访问变量

also, look into jQuery .ajax() for simpler code regarding AJAX

另外,请查看jQuery .ajax()以获取有关AJAX的更简单代码

#5


2  

You've defined a inside the getResults() function using the 'var' keyword. This means it will only be in scope inside that function.

您已使用'var'关键字在getResults()函数内定义了一个。这意味着它只会在该函数内的范围内。

If you want to use that variable in another function, you'll need to pass it across when you call the other function - simple example is below:

如果你想在另一个函数中使用那个变量,你需要在调用另一个函数时传递它 - 简单的例子如下:

function myFunction() {
 var a = "this is a";
 secondFunction(a);
}

function secondFunction(a) {
 alert(a);
}

An alternative is to declare the variable so it has global scope - but that's not a great idea unless you're confident that it won't conflict with any other global variables that might exist on your page.

另一种方法是声明变量,使其具有全局范围 - 但这不是一个好主意,除非您确信它不会与页面上可能存在的任何其他全局变量冲突。

You're better off constraining scope unless you absolutely need to have global variables. It'll make your code neater, and debugging easier.

除非绝对需要全局变量,否则最好限制范围。它会使您的代码更整洁,调试更容易。