2

我写代码。我正在尝试编写一个表单,该表单在“iframe”中包含一长串单选按钮内容,如下所示:

<form name="iframeform" method="POST" action="http://localhost/cgi-bin/run">
    <tr>
            <td><iframe id="macList" src="http://localhost/macinfo" style="width:1000px;height:160px">
            </iframe></td>
    </tr>
    <tr>
            <input type="hidden" name="macaddr" value="">
            <td><input type="submit" name="macselect" value="Continue" id="stdbtn" onclick="getIframeContent()"></td>
    </tr>
</form>

'iframe' 指向 macinfo 页面的代码如下:

<html>
<body>
<table>
<tr>
<td><input type=radio name=mac value=E8:B7:48:7B:C0:4A>abc.example.com</td>
</tr>
<tr>
<td><input type=radio name=mac value=00:17:08:5A:81:CF>xyz.cisco.com</td>
</tr>
....
</table>
</body>
</html>

如何编写“getIframeContent()”函数来获取选定单选按钮的值?请帮忙..

4

2 回答 2

0

您将必须获取macusing数组document.getElementsByName并对其进行循环:

var allRadios = document.getElementsByName("mac");
var checkedItem; 
for(var i=0; i<allRadios.length; i++) {
     if (allRadios[i].checked) {
          checkedItem = allRadios[i];
          alert('Found checked radio button');
     }
 }

然后你可以传递checkedItem给你的getIframeContent函数。

编辑

您可以通过使用在两个页面之间共享checkedItem变量window

window.checkedItem = allRadios[i];
于 2013-02-22T13:32:04.517 回答
0

长话短说。要访问 iframedocument使用.contentWindow.document,即:

document.getElementById('macList').contentWindow.document

还有一个示例代码:

<html>
  <head>
    <script type="text/javascript">
      function getIframeContent(){
         var myIFrame = document.getElementById('macList');
         var myIFDoc  = myIFrame.contentWindow.document;
         var myRadios = myIFDoc.getElementsByName("mac");

         var checkedItemValue = "";
         for(var i=0; i<myRadios.length; i++) {
            if (myRadios[i].checked) {
               checkedItemValue = myRadios[i].value;
            }
         }
         if (checkedItemValue) {
            alert(checkedItemValue);
         } else {alert('Select address');}
      }
    </script>
  </head>
<body>

  <iframe id="macList" src="..." 
      style="width:600px;height:160px">
  </iframe>

  <input type="submit" onclick="getIframeContent();">

</body>
</html>
于 2013-02-22T14:25:47.113 回答