Get value of a text box in iframe from parent wind

2019-02-16 13:50发布

问题:

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?

here is my code

<div>
<iframe src="test.html" >
<input type=text id="parent_text">
<input type="button">
</div>

here is d test.html

<input type="text" id="frame_text">

thanks

回答1:

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.

And finaly get the input box.



回答2:

<!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>


回答3:

If you have Div/ input as follows in one Iframe

 <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 Div using the following code

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.



回答4:

$('iframe').contents().find('#mytextbox').val("myvalue");


回答5:

Here is parent page code (I mean main page call to 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>