访问iFrame中的HTML控件

时间:2013-03-20 23:50:41

标签: dynamics-crm-2011

如何从CRM中的javascript访问iframe中的html控件?

我有:

 var height = document.getElementById("IFRAME_TransactionProduct_RA").contentWindow.document.getElementById("txt").value;

但是这会导致页面上出现“#34;错误”#34;并且未加载内容。

我要访问的元素是一个html输入,其id为' txt':

 <input id="txt" type="hidden" />

2 个答案:

答案 0 :(得分:4)

以下是如何将值从CRM字段复制到IFRAME中嵌入式HTML控件中的控件的示例。我假设网络资源和字段的名称。你必须适应那些。你也可以投入一个 try-catch 以防CRM引发异常(得到了笑话?)并且请注意我在手机上输入代码所以可能强>在某处是一个错字(自动纠正,是的)。

var source = Xrm.Page.data.entity.attributes.get("oneCoolField")
var information = source.getValue();

var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");
var destination = customHtml.getObject().contentWindow.document;
if(destination) {
  var customControl = destination.getElementById("elementToAccess");
  if(customControl) {
    customControl.value = information;
  }
}

编辑:

这可以让您访问网络资源。

var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");

这将引导您进入IFRAME的DOM。

var destination = customHtml.getObject().contentWindow.document;

这可以让您进入自定义页面的控件。

var customControl = destination.getElementById("elementToAccess");

这可以获取控件的内容。

var contents = customControl.innerHTML;

您的计算机上哪个部分失败了?

答案 1 :(得分:1)

使用jQuery:

$(Xrm.Page.ui.controls.get('IFRAME_TransactionProduct_RA').getObject()).contents().find('#txt').val();

纯JS:

Xrm.Page.ui.controls.get('IFRAME_TransactionProduct_RA').getObject().contentWindow.document.getElementById('txt').value;

http://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_getObject