javascript 如何使用javascript函数将值传递给弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13967099/
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
how to pass value to pop up window using javascript function?
提问by varadaraj
I want to open new window from existing form.When I am clicking on new window in form then new form should open with results of value entered,but new window form is opening with main page,not able to get text box value from parent form.
我想从现有表单打开新窗口。当我点击表单中的新窗口时,新表单应该打开并输入值的结果,但新窗口表单正在打开主页,无法从父表单获取文本框值.
How to call textfield input value in popup window from parent window using javascript?
如何使用javascript从父窗口调用弹出窗口中的文本字段输入值?
//currently I'm using following code:
<script type="text/javascript">
function pop_up5() {
var l_url=window.opener.document.getElementById("name").value;
window.open(l_url,'''','scrollbars=yes,resizable=no,width=550,height=400');
}
</script>
回答by chris
Suppose your text field's id is myTextField
, whatever you named it. if it has no id, set a id for it. then, in the popup window, you can use JavaScript parent.document.getElementById('myTextField').value
to get the value of the textfield.
假设您的文本字段的 id 是myTextField
,无论您如何命名它。如果它没有 id,则为其设置一个 id。然后,在弹出窗口中,您可以使用 JavaScriptparent.document.getElementById('myTextField').value
获取文本字段的值。
回答by gkiely
Assign window.open()
to a variable so you can access it's elements.
分配window.open()
给一个变量,以便您可以访问它的元素。
<form>
<input type="textbox" id="txt" />
<input type="button" id="btn" value="Open window" />
</form>
<script>
document.getElementById('btn').onclick = function(){
var myWindow = window.open();
myWindow.document.body.innerHTML = document.getElementById('txt').value;
};
</script>