简体   繁体   English

无法将值从window.opener传递给其父级

[英]Can't pass value from window.opener to its parent

I have a button that when pressed opens a new window, in that window the user chooses a file and clicks it. 我有一个按钮,当按下该按钮时会打开一个新窗口,用户在该窗口中选择一个文件并单击它。 When clicked, the value should pass to a hidden input in the form and closes the window. 单击时,该值应传递到表单中的隐藏输入并关闭窗口。 Everything acts like it should be, the only thing that its not working, is the fact that the input value stays empty. 一切都按应有的方式运行,唯一不起作用的是输入值保持为空。

This is my input: 这是我的输入:

<input name="value_pdf" value="" type="hidden" id="value_pdf" />

And here is the function: 这是函数:

$(".insert").click(
    function () {
            var file = $(this).attr("rel"); //its correct and passes the value I want
            var value = "value_<?php echo $_GET['but'];?>" //again, this is correct too, I've used console.log() to check

            opener.$("#value_pdf").val(file);
            //I've done a console.log() here to to check if #value_pdf had a value and it was correct, it logged something like "database.pdf"

            window.close();
            return false;
    }
);

My question is, where did I do it wrong? 我的问题是,我在哪里做错了? I seems that everything is right... 我似乎一切都很好...

EDIT: Turns out, somewhere in my code I had this same ID on a DIV. 编辑:结果,在我的代码中的某个地方,我在DIV上有这个相同的ID。 Althought I advise you to check the choosen answer if you have this same problem, it made my code better. 虽然我建议您在遇到同样问题时也要检查选择的答案,但这使我的代码更好。

I've never tried opener.$(...) but it looks like it should work, providing the opener window still exists and there's no cross-domain issue. 我从未尝试过opener.$(...)但它似乎应该可以工作,前提是该opener窗口仍然存在并且没有跨域问题。

If it doesn't work then you might try writing a function in the opener and calling it from the child window. 如果不起作用,则可以尝试在打开器中编写一个函数,然后从子窗口中调用它。

In the opener (in the global namespace or in some namespace accessible from global) : 在打开程序中(在全局名称空间中或在可从全局访问的某些名称空间中):

function setValue(selector, value) {
    $(selector).val(value);
}

In the child window : 在子窗口中:

window.opener.setValue("#value_pdf", file);

I can't really see why this would work when opener.$(...) didn't because there's no fundamental difference. 我真不明白为什么在opener.$(...)没用,因为没有根本的区别。 But hey, you never know ... 但是,嘿,你永远不知道...

If you have php enabled I would use that instead of sending via javascript. 如果您启用了php,我会使用它而不是通过javascript发送。 And if you need the information on the same page, i would use AJAX. 如果您需要同一页面上的信息,我将使用AJAX。

http://api.jquery.com/jquery.post/ http://api.jquery.com/jquery.post/

a sample would look like this. 样本看起来像这样。

 $.post( "test.php", $( "#testform" ).serialize() ); 
 <form id="testform"> <input name="something"> </form> 

This would take a form and submit the input named "something" and post it to the "test.php" page. 这将采用一个表单,并提交名为“ something”的输入,并将其发布到“ test.php”页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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