javascript 将值从子窗口传递到父窗口中的文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19368842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:20:09  来源:igfitidea点击:

Passing a value from child window to text field in parent window

javascript

提问by Simon

I'm trying to pass and insert value from child window to the value attributeof text field which located in parent window, and not to its property. I've tried all the following but no luck so far:

我试图将值从子窗口传递并插入到位于父窗口中的文本字段的value 属性,而不是它的property。我已经尝试了以下所有方法,但到目前为止没有运气:

Child window:

子窗口:

function getFile(oImg){
var id = <?php echo $id; ?>;
var editPage = window.opener.document;
oSrc = oImg.src;
lastSlash = oSrc.lastIndexOf('/');
oName = oSrc.substr(lastSlash+1);
var logo = 'logo'+id, logoHolder = 'logoHolder'+id;
//window.opener.document.getElementById(logo).value = oName;
//window.opener.document.getElementById(logo).setAttribute("value", oName);
//window.opener.document.getElementById(logo).innerHTML = oName;
//window.opener.document.getElementById(logo).setValue = oName;
window.opener.document.getElementById(logoHolder).src = "templates/img/user/" + oName;
this.close()
}

Parent page:

父页面:

<input type="text" id="logo1" name="logo1" value="VALUE-SHOULD-BE-INSERTED-HERE" />

<input type="text" id="logo1" name="logo1" value="VALUE-SHOULD-BE-INSERTED-HERE" />

The first one just display the value in the text-field but when I'm trying to save it with jquery, the value is actually empty. Any idea?

第一个只是在文本字段中显示值,但是当我尝试使用 jquery 保存它时,该值实际上是空的。任何的想法?

回答by poke

window.opener.document.getElementById(logo).value = fileName;

This should work—except that logoshould be a string. Otherwise the opened child window will try to find a variable with the name logo, which it will probably not find.

这应该可以工作——除了它logo应该是一个字符串。否则,打开的子窗口将尝试查找名称为 的变量logo,它可能找不到。

So, do this:

所以,这样做:

window.opener.document.getElementById('logo').value = fileName;


No idea what you are doing wrong, maybe the PHP echo won't output the correct ID? Anyway, it works fine for me. See this example:

不知道你做错了什么,也许 PHP echo 不会输出正确的 ID?无论如何,它对我来说很好用。看这个例子:

parent.html

父.html

<input type="text" id="logo1" value="VALUE-SHOULD-BE-INSERTED-HERE" />
<script>window.open('child.html');</script>

child.html

孩子.html

<button id="btn">Click me</button>
<script>
btn.addEventListener('click', function () {
    var logo = 'logo' + '1';
    window.opener.document.getElementById(logo).value = 'something';
    window.close();
});
</script>

回答by Abacus

The better way to do this is now Window.postMessage(), see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessagefor details.

现在更好的方法是 Window.postMessage(),详情请参见https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

回答by Smeegs

I'm not sure what's causing the issue, but maybe you can try to update via a function in the parent called by the child.

我不确定是什么导致了问题,但也许您可以尝试通过孩子调用的父级中的函数进行更新。

So in the parent add the function (I assume you can use jQuery, because you mentioned it in the question)

所以在父级添加函数(我假设你可以使用 jQuery,因为你在问题中提到了它)

function UpdateValue(newVal){
     $("#logo").val(newVal); 
}

And call it in the child

并在孩子中调用它

window.opener.UpdateValue(fileName);

window.opener.UpdateValue(fileName);

Although I would like to know why the original isn't working. This might be a good workaround.

虽然我想知道为什么原来的不起作用。这可能是一个很好的解决方法。