Javascript 从父窗口获取 iframe 中文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11330933/
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
Get value of a text box in iframe from parent window
提问by raki
I have an iframe in my page and how can i get a the value of t text box in the frame from my parent page on a button click?
我的页面中有一个 iframe,如何通过单击按钮从父页面获取框架中 t 文本框的值?
here is my code
这是我的代码
<div>
<iframe src="test.html" >
<input type=text id="parent_text">
<input type="button">
</div>
here is d test.html
这是 d test.html
<input type="text" id="frame_text">
thanks
谢谢
回答by Niclas Larsson
Something like this:
像这样的东西:
var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var input = innerDoc.getElementById('frame_text');
First you get the the iframe. Then you get the first valid dom document from inside the iframe.
首先你得到iframe。然后您从 iframe 中获得第一个有效的 dom 文档。
And finaly get the input box.
最后得到输入框。
回答by Flea
$('iframe').contents().find('#mytextbox').val("myvalue");
回答by Michael Besteck
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function getIframeText() {
var iframe0 = document.getElementById("iframe0");
var iframe0document=iframe0.contentDocument||iframe0.contentWindow.document;
var inputIframe = iframe0document.getElementById("frame_text");
alert(inputIframe.value);
}
</script>
</head>
<body>
<div>
<button onclick="getIframeText()">get iframe text</button>
<iframe id="iframe0" src="test.html" >
<input type=text id="parent_text">
</div>
</body>
</html>
回答by Md. Nazrul Islam
If you have Div/ inputas follows in one Iframe
如果您在一个Iframe中有如下Div/ 输入
<iframe id="ifrmReportViewer" name="ifrmReportViewer" frameborder="0" width="980"
<div id="EndLetterSequenceNoToShow" runat="server"> 11441551 </div> Or
<input id="txtSequence" type="text" value="10500101" />
<form id="form1" runat="server">
<div style="clear: both; width: 998px; margin: 0 auto;" id="divInnerForm">
Some Text
</div>
</form>
</iframe>
Then you can find the text of those Divusing the following code
然后你可以使用以下代码找到那些Div的文本
var iContentBody = $("#ifrmReportViewer").contents().find("body");
var endLetterSequenceNo = iContentBody.find("#EndLetterSequenceNoToShow").text();
var txtSequence = iContentBody.find("#txtSequence").val();
var divInnerFormText = iContentBody.find("#divInnerForm").text();
I hope this will help someone.
我希望这会帮助某人。
回答by Rakesh Samal
Here is parent page code (I mean main page call to iframe
):
这是父页面代码(我的意思是主页面调用iframe
):
<script>
function frameClick(string){
var name=string;
document.getElementById("parent_text")=name;
}
</script>
<div>
<iframe src="test.html" id="frame1">
<input type=text id="parent_text">
</div>
Here is iframe code(test.html):
<form method="post" action="" id='frm1' >
<input type="text" id="frame_text">
<input type="button" id="btm1" name="hangup" onclick="parent.frameClick(this.form.frame_text.value);" value="submit">
</form>