【问题标题】:add text to iframe inputbox将文本添加到 iframe 输入框
【发布时间】:2014-01-10 00:03:20
【问题描述】:

请帮助我使用 iframe 触发器。

如何将值放入 iframe 的输入字段中? 我相信问题的存在是因为布局级别。

(iframe作为html放在我自己的网站上)

我的 iframe:

<form id="form">
<div id="root">
<iframe id="lev-iframe">
<html>
<form id="form">
<fieldset class="fields">      
  <div class="full">
  <div class="part">
     <input class="uniqueclass" name="uniquename" type="text" value="" />
  </div></div>

jQuery

//tested #root, #form, #lev-iframe; nothing seems to work for me...

<script type="text/javascript">
$(window).bind("load", function() {

$('#root').trigger('setFieldValue', {
name: 'uniquename',
value: 'myvalue'

})  }); 
</script>

【问题讨论】:

  • 这段代码很奇怪。
  • 哈哈对不起。希望您指的是 iframe 代码而不是触发器。我想我已经接近解决它了,但目前它对我没有任何作用。
  • 我认为你需要学习 HTML 的基础知识。

标签: javascript jquery iframe input


【解决方案1】:

您不能像在示例中那样通过在 IFRAME 标记中添加 HTML 来定义 IFRAME。它必须指向具有此 HTML 的另一个页面:

<iframe id="lev-iframe" src="other_page.html"></iframe>

或以编程方式用 HTML 填充它,例如:

HTML

<iframe id="lev-iframe"></iframe>

JS

//This code will add input box to the iframe above
var doc = $("#lev-iframe")[0].contentWindow.document;
var $body = $('body',doc);
$body.html('<input class="uniqueclass" id="uniquename" type="text" value="" />'); 

一旦您拥有一个有效的 IFRAME,就可以轻松访问所需的元素:

$("#lev-iframe")[0].contentWindow.document.getElementById('uniquename').value = 'myvalue';

演示:http://jsfiddle.net/wbwCu/

【讨论】:

  • 感谢您的建议。是否可以将值打印到输入框(setFieldValue)而不是添加一个全新的输入字段?
  • 添加整个输入字段只是以编程方式创建 IFRAME 内容的一个示例 - 您不必这样做。如果您像第一个示例中那样通过“SRC =”定义 IFRAME 的 HTML,那么您需要设置的值就是最后一行代码。
  • 感谢 Yuriy,我设法更新了值并最终集成了新的输入字段。
猜你喜欢
  • 1970-01-01
  • 2020-03-12
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多