简体   繁体   中英

Passing variables between jQuery and other javascript global variables

I'm having trouble letting functions I defined in jquery to read javascript values i defined elsewhere. for example I have a function defined within my jquery

var parentImg = ''; //global variable.

$(document).change(function() {
  if ($("#parentImage option:selected").val())
    parentImg = $("#parentImage option:selected").val();
    alert(parentImg);
});

$(function(){
    $('#swfuploadcontrol').swfupload({
        upload_url: "upload-file.php?page=<?php echo $htmlFile; ?>&parentImg=" + parentImg});})

but I can't let it read a global variable I defined at top ""parentImg"". It seems like everything defined within jquery lives in its own space and doesn't interact with the outside world..

any idea of how i can pass these values back and forth btw jquery and the rest of the code?

How can I pass "parentImg" variable value long with "upload_url" variable..?

You're setting a variable available to both just fine...jQuery doesn't in any way have it's own variable, it's all JavaScript. The problem is the order in which your code is firing, are the document.ready handler bound in the right order?

Right now you're binding to the change event on document (presumably to capture bubbling change events?...this could be right on the element), but that'll run after your .swfupload() call (which runs on document.ready ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM