简体   繁体   中英

Using jquery from parent in a child iframe

i want to use jquery library in a child iframe. Parent window has included jquery library. This is the script that included in iframe

$p=parent.window.$;
$p(document).ready(function (){
        console.log($p('#bla').val()); 
        console.log($p('#searchedCity').val());
    }
);

$p('#bla').val() - this I'm trying to get value of input text field that is located in iframe. Now it returns undefined

$p('#searchedCity').val() - this is value of input text field from parent window. and it returns the right value

when I'm including jquery library directly to the iframe - $('#bla').val() works fine.

what am I missing? Can you help me?

I don't know if it does the same but try this :

console.log($('#bla', p).val()); 
console.log($('#searchedCity', p).val());

that is because your searching for elements $p('#bla') #bla in parent container. which is not present there..load jquery in current iframe and use $ ..

$p=parent.window.$;
$p(document).ready(function (){
    console.log($('#bla').val()); 
    console.log($p('#searchedCity').val());
   }
);

and yes you are calling these function when parent's document is ready which may not work as it should..

$(document).ready(function (){
    console.log($('#bla').val()); 
     $p=parent.window.$;
    console.log($p('#searchedCity').val());
   }
);

Try this:

$p=parent.window.$;
    $p(document).ready(function (){
        console.log($('#bla').val()); 
        console.log($p('#searchedCity').val());
    }
);

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