javascript - How to get global variable value to ajax - Stack Overflow

i am trying to get the global variable value inside ajax,jquery function.here i am using this code..cod

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

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

<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'

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

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 ing correctly) but the alert from the ajax,jquery is ing is as undefined or [Object Object]

How can i resolve this??

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

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

<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'

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

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 ing correctly) but the alert from the ajax,jquery is ing is as undefined or [Object Object]

How can i resolve this??

Share Improve this question asked Sep 25, 2013 at 12:38 MintYMintY 6435 gold badges12 silver badges23 bronze badges 1
  • now that id value will be set to some value - where? – Rameez Ahmed Sayad Commented Sep 25, 2013 at 12:45
Add a ment  | 

3 Answers 3

Reset to default 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.

Just modify as below,

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

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:

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744682165a4587693.html

相关推荐

  • javascript - How to get global variable value to ajax - Stack Overflow

    i am trying to get the global variable value inside ajax,jquery function.here i am using this code..cod

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信