如何将数据发布到iframe?

有帮助吗?

解决方案

取决于你的意思<!>“发布数据<!>”;您可以在target=""标记上使用HTML <form />属性,因此它可以像以下一样简单:

<form action="do_stuff.aspx" method="post" target="my_iframe">
  <input type="submit" value="Do Stuff!" />
</form>

<!-- when the form is submitted, the server response will appear in this iframe -->
<iframe name="my_iframe" src="not_submitted_yet.aspx"></iframe>

如果不是这样,或者您正在处理更复杂的事情,请编辑您的问题以包含更多详细信息。

Internet Explorer存在一个已知错误,只有在您使用Javascript动态创建iframe等时才会出现(有一个在这里解决方法),但是如果你使用的是普通的HTML标记,你就可以了。目标属性和框架名称不是一些聪明的忍者黑客;虽然它在HTML 4 Strict或XHTML 1 Strict中被弃用(因此不会验证),但它自3.2起就成为了HTML的一部分,它正式成为HTML5的一部分,并且它自Netscape 3起几乎适用于所有浏览器。

我已将此行为验证为使用XHTML 1 Strict,XHTML 1 Transitional,HTML 4 Strict和<!> quot; quirks mode <!>没有指定DOCTYPE,它适用于所有使用Internet Explorer 7.0.5730.13的情况。我的测试用例包含两个文件,在IIS 6上使用经典ASP;它们在这里完整复制,因此您可以自己验证这种行为。

<强>的default.asp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Form Iframe Demo</title>
  </head>
  <body>
  <form action="do_stuff.asp" method="post" target="my_frame">
    <input type="text" name="someText" value="Some Text" />
    <input type="submit" />
  </form>
  <iframe name="my_frame" src="do_stuff.asp">
  </iframe>
  </body>
</html>

<强> do_stuff.asp

<%@Language="JScript"%><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Form Iframe Demo</title>
  </head>
  <body>
  <% if (Request.Form.Count) { %>
  You typed: <%=Request.Form("someText").Item%>
  <% } else { %>
  (not submitted)
  <% } %>
  </body>
</html>

我很想知道任何浏览器没有正确运行这些示例。

其他提示

iframe用于在html页面中嵌入另一个文档。

如果要将表单提交到表单页面中的iframe,那么可以使用标记的target属性轻松实现该表单。

将表单的target属性设置为iframe标记的名称。

<form action="action" method="post" target="output_frame">
    <!-- input elements here --> 
</form>
<iframe name="output_frame" src="" id="output_frame" width="XX" height="YY">
</iframe>           

使用高级iframe目标
此属性也可用于产生类似ajax的体验,特别是在文件上传等情况下,在这种情况下,必须提交表单,以便上传文件

可以将iframe设置为0的宽度和高度,并且可以在将目标设置为iframe的情况下提交表单,并在提交表单之前打开加载对话框。所以,它控制了一个ajax控件,因为控件仍然保留在输入格式jsp上,打开加载对话框。

〔实施例

<script>
$( "#uploadDialog" ).dialog({ autoOpen: false, modal: true, closeOnEscape: false,                 
            open: function(event, ui) { jQuery('.ui-dialog-titlebar-close').hide(); } });

function startUpload()
{            
    $("#uploadDialog").dialog("open");
}

function stopUpload()
{            
    $("#uploadDialog").dialog("close");
}
</script>

<div id="uploadDialog" title="Please Wait!!!">
            <center>
            <img src="/imagePath/loading.gif" width="100" height="100"/>
            <br/>
            Loading Details...
            </center>
 </div>

<FORM  ENCTYPE="multipart/form-data" ACTION="Action" METHOD="POST" target="upload_target" onsubmit="startUpload()"> 
<!-- input file elements here--> 
</FORM>

<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;" onload="stopUpload()">   
        </iframe>

此函数创建一个临时表单,然后使用jQuery发送数据:

function postToIframe(data,url,target){
    $('body').append('<form action="'+url+'" method="post" target="'+target+'" id="postToIframe"></form>');
    $.each(data,function(n,v){
        $('#postToIframe').append('<input type="hidden" name="'+n+'" value="'+v+'" />');
    });
    $('#postToIframe').submit().remove();
}

target是目标iFrame的'name'attr,data是JS对象:

data={last_name:'Smith',first_name:'John'}

如果您想更改iframe中的输入,请从该iframe提交表单,执行此操作

...
var el = document.getElementById('targetFrame');

var doc, frame_win = getIframeWindow(el); // getIframeWindow is defined below

if (frame_win) {
  doc = (window.contentDocument || window.document);
}

if (doc) {
  doc.forms[0].someInputName.value = someValue;
  ...
  doc.forms[0].submit();
}
...

通常情况下,只有iframe中的页面来自同一个来源才能执行此操作,但您可以在调试模式下启动Chrome以忽略相同的原始策略并在任何页面上对此进行测试。

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top