Javascript 在子窗口中访问父局部变量

Javascript 在子窗口中访问父局部变量,javascript,php,global-variables,parent-child,Javascript,Php,Global Variables,Parent Child,我想在子窗口中使用父对象的局部变量。我使用了parent.window.opener,但它返回undefined <script type="text/javascript"> var selectedVal; $(document).ready(function () { //.... //... if ($(this).val() == "byActor"){ $("#tags").focus(); $("#tags

我想在子窗口中使用父对象的局部变量。我使用了
parent.window.opener
,但它返回
undefined

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
这是我的代码:

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>

var-selectedVal;
$(文档).ready(函数(){
//....
//...
if($(this.val()=“byActor”){
$(“#标记”).focus();
$(“#标记”).autocomplete({
来源:“actorsauto.php”,
最小长度:2,
焦点:功能(事件、用户界面){
event.preventDefault();
返回false;
},
选择:函数(事件,ui){
var selectedVal=ui.item.value;
警报(selectedVal);
}
}); 
});
$('btnRight')。在('click',函数(e)上{
popupCenter(“movieByactor.php”,“_blank”,“400”,“400”);
});
这是一个孩子:

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
<body>
 <script type="text/javascript">

  var selectedVal = parent.window.opener.selectedVal; 
   alert(selectedVal);

 </script>
</body>

var selectedVal=parent.window.opener.selectedVal;
警报(selectedVal);
试试这个:

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
<body>
    <script type="text/javascript">

        var selectedVal = window.opener.selectedVal; 
        alert(selectedVal);

    </script>
</body>

var selectedVal=window.opener.selectedVal;
警报(selectedVal);

你不能-局部变量的整体思想是它们只在声明它们的任何函数范围内可用-以及在该函数内的函数

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
在您的情况下,select
selectedVal
仅在此函数声明中可用:

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
select: function (event, ui){ 
   var selectedVal = ui.item.value;
   alert(selectedVal);
}
要在此范围之外使用它,您需要通过将其附加到窗口使其成为全局的:

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
window.selectedVal = 'somevalue';
您还可以通过省略
var
关键字使变量隐式全局化,但是这是一种糟糕的做法,在严格模式下是不允许的

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
这将允许您访问
窗口。通过以下方式选择Val

<script type="text/javascript">
 var selectedVal;

 $(document).ready(function () {
  //....
  //...
   if ($(this).val() == "byActor"){
           $("#tags").focus();
           $("#tags").autocomplete({
             source: "actorsauto.php",
             minLength: 2,
             focus: function( event, ui ){
                   event.preventDefault(); 
                   return false;
             },
             select: function (event, ui){ 
                       var selectedVal = ui.item.value;
                       alert(selectedVal);
                   }
            }); 
   });

$('#btnRight').on('click', function (e) {
         popupCenter("movieByactor.php","_blank","400","400");
});
</script>
 </body>
 </html>
window.opener.selectedVal // for windows opened with window.open()
window.parent.selectedVal // iframe parent document

你确定你既想要
父项
又想要
开启器
吗?另外,你是否试图在设置值之前阅读
selectedValue
?谢谢你的回答。那么,你的意思是我必须写
window.selectedVal=ui.item.value;
而不是
var selectedVal=ui.item.value;
?非常感谢你的回答完整的解释,有效:)