简体   繁体   中英

Get form content from iFrame

I have a form, 2 questions are in separate iFrames, these options change depending on what options are chosen on the parent frame (they auto refresh when certain options are chosen), how can I get the option chosen in the iFrames back in to the parent frame to pass on when the form is submitted? Parent snippet:

<td>Manager Name:</td>
<td><iframe name="manname" id="manname" frameborder="0" width="300" height="21" scrolling="no" src="./get_manager.php"></iframe>
<input type="hidden" id="starter_manname" name="starter_manname" width="300">
</td>

I'm wanting starter_manname to contain what is chosen from the iFrame. This is part of the iFrame content:

<select id="starter_manname" name="starter_manname" width="300" class="textboxstyle">
<option selected value="">----- Select Manager -----</option>
<option value="CouldBeAnythingVal">CouldBeAnythingName</option>
</select>

the iFrames just contain the <html> , <body> & <option> tags, there's no <form> or anything in there. I'm useless with Javascript and I assume that's the answer so if you can explain the answer or point me in the correct direction it would be appreciated (without jQuery though please)

You can access the iframe contents using jquery's .contents() method. If you have an iframe with id "myframe" then, you can do:

    $('#myframe').contents().find('something')
    // or event bind events
    $('#myframe').contents().find('button').on('click', function(){  });

Assuming that you have content inside iframe <iframe name="manname" id="manname" frameborder="0" width="300" height="21" scrolling="no" src="./get_manager.php"></iframe>

$(function(){
  var selectInframe = $('#manname').contents().find('#starter_manname');
  selectInframe.on('change', function(){
    var new_val = $(this).val();
    $('#starter_manname').val(starter_manname);
  });
});

without jquery you can you contentDocument

var selectInframe = document.getElementById('manname');
var selectInframeDocument = selectInframe.contentDocument;
// selectInframeDocument is the document form your iframe
// you can do selectInframeDocument.getElementByTagName('..') etc to access elements and bind

Hope you get it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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