简体   繁体   English

使用JavaScript从字段设置iframe值

[英]Set Iframe Value From Field Using JavaScript

I have a CRM form with an iframe on it that contains an HTML textarea (id=textbox). 我有一个带有iframe的CRM表单,其中包含HTML文本区域(id = textbox)。 On the form, I have a field called "Justification". 在表单上,​​我有一个名为“正当化”的字段。 I need the textarea value that is in the iframe, to be set to the value that is in the "Justification" field on the parent form Onload using javascript. 我需要将iframe中的textarea值设置为使用javascript在父表单Onload上的“ Justification”字段中的值。 is there a simple way to do this, and if so, can you provide a script? 有没有一种简单的方法可以执行此操作,如果可以,您可以提供一个脚本吗? Thank you in advance, any help would be appreciated 预先谢谢您,任何帮助将不胜感激

There is quite a few different way to accomplish that goal, since you state a onload event in your tags, you could go with: 有多种方法可以实现该目标,因为您在代码中声明了onload事件,因此可以使用:

var parentValue = parent.document.getElementById("Justification").value;
document.getElementById("textbox").value = parentValue;

If the iframe src and the parent exist on the same domain you can use the contentWindow of the iframe to access the DOM elements. 如果iframe src和父项位于同一个域中,则可以使用iframe的contentWindow访问DOM元素。 MDN contentWindow MDN contentWindow

If they don't exist on the same domain then the browser will prevent you from manipulating the DOM due to cross-origin restrictions. 如果它们不在同一域中,则由于跨域限制,浏览器将阻止您操作DOM。

If you have access to the source code for the parent and the iframe you could implement window.postMessage() to pass data between the parent and iframe. 如果您可以访问父级和iframe的源代码,则可以实现window.postMessage()在父级和iframe之间传递数据。 MDN postMessage MDN postMessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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