如何获取ajax的全局变量值

时间:2022-08-25 14:03:32

i am trying to get the global variable value inside ajax,jquery function.

我想在ajax,jquery函数中获取全局变量值。

here i am using this code..

我在这里使用这段代码..

code:

 <script type="text/javascript">

        var id;      
        function refreshRecord(id)
        {
           alert(id);
        }

     $(document).ready(function(){
     $("#refresh").click(function(){
         var fileId=id;
         alert("id is"+fileId);
         $.ajax({
            type:"post",
            url:"checkStatusAndNumRecs",
            data: {fileId:fileId},
            success:function(data){$("#div1").html(data);},
            error:function(data){$("#div1").html("It was a failure !!!");}
            });
            });
            }); 
</script>   

Onclick one submit button i am calling the javascript function

点击一个提交按钮我正在调用javascript函数

<input type="radio" name="submit" value="submit" onclick="refreshRecord(this.value)">

Here what i want to get is, i have declared the global variable id in script tag, when i click on radio button the onclick event calls the javascript function refreshRecord(id) with one parameter 'id'

这里我想得到的是,我已经在脚本标签中声明了全局变量id,当我点击单选按钮时,onclick事件使用一个参数'id'调用javascript函数refreshRecord(id)

now that id value will be set to some value. now i want to get that variable value inside jquery function and i want to assign it to

现在,id值将被设置为某个值。现在我想在jquery函数中获取该变量值,我想将其分配给

var fileId = id;

but when i did the above code and click button.

但是当我做了上面的代码并点击按钮。

in alert it is showing the first value correctly(i.e the alert from javascript is coming correctly) but the alert from the ajax,jquery is coming is as undefined or [Object Object]

在警报中它正确显示第一个值(即来自javascript的警报正确到来)但来自ajax的警报,jquery即将到来是未定义的或[对象对象]

How can i resolve this??

我怎么解决这个?

3 个解决方案

#1


7  

You need to assign the value passed to the function to the global variable id. Currently you are not assigning it. The parameter id of the function is just local to the function , it is not the same global one and the global variable id is still undefined.

您需要将传递给函数的值分配给全局变量id。目前你没有分配它。函数的参数id只是函数的本地参数,它不是全局函数,全局变量id仍未定义。

Just modify as below,

只需修改如下,

    var id;      
    function refreshRecord(value)
    {
       id = value;
       alert(id);
    }

#2


2  

You should put the value from the field inside the global variable:

您应该将字段中的值放在全局变量中:

var id;      
function refreshRecord(inputValue)
{
    id = inputValue;
    alert(id);
}

Because if you do this:

因为如果你这样做:

function refreshRecord(id)
{
    alert(id);
}

the id global variable won't be accessible inside the refreshRecord function because of a name conflict inside the function scope. Please read about scope to understand:

由于函数范围内存在名称冲突,因此无法在refreshRecord函数内访问id全局变量。请阅读有关范围以了解:

http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

#3


0  

The argument we pass inside a function acts as a local variable to that function. So in your function you are actually creating a local variable and setting the value.

我们在函数内传递的参数充当该函数的局部变量。因此,在您的函数中,您实际上是在创建局部变量并设置值。

Try this instead -

试试这个 -

var id;
function refreshRecord() {
    id = $("input:radio").val();
}

Give an id to have better selector for radio button.

给一个id以获得更好的单选按钮选择器。

#1


7  

You need to assign the value passed to the function to the global variable id. Currently you are not assigning it. The parameter id of the function is just local to the function , it is not the same global one and the global variable id is still undefined.

您需要将传递给函数的值分配给全局变量id。目前你没有分配它。函数的参数id只是函数的本地参数,它不是全局函数,全局变量id仍未定义。

Just modify as below,

只需修改如下,

    var id;      
    function refreshRecord(value)
    {
       id = value;
       alert(id);
    }

#2


2  

You should put the value from the field inside the global variable:

您应该将字段中的值放在全局变量中:

var id;      
function refreshRecord(inputValue)
{
    id = inputValue;
    alert(id);
}

Because if you do this:

因为如果你这样做:

function refreshRecord(id)
{
    alert(id);
}

the id global variable won't be accessible inside the refreshRecord function because of a name conflict inside the function scope. Please read about scope to understand:

由于函数范围内存在名称冲突,因此无法在refreshRecord函数内访问id全局变量。请阅读有关范围以了解:

http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

#3


0  

The argument we pass inside a function acts as a local variable to that function. So in your function you are actually creating a local variable and setting the value.

我们在函数内传递的参数充当该函数的局部变量。因此,在您的函数中,您实际上是在创建局部变量并设置值。

Try this instead -

试试这个 -

var id;
function refreshRecord() {
    id = $("input:radio").val();
}

Give an id to have better selector for radio button.

给一个id以获得更好的单选按钮选择器。