简体   繁体   English

单击iframe外部的链接时,请在iframe中填充输入字段

[英]Fill input field inside an iframe when link outside of iframe is clicked

I have been trying to get this to work for 2 weeks now but I am stumped on this which is probably very simple. 我一直在尝试使其工作2周,但是我对此感到很困惑,这可能非常简单。 I am new to js. 我是js新手。

I have multiple links on a page along with a form embedded in an iframe. 我在页面上有多个链接,以及在iframe中嵌入的表单。 Same domain, same page, both using https. 相同的域,相同的页面,都使用https。 The form is from a form script and is embedded in the page using: 该表单来自表单脚本,并使用以下命令嵌入到页面中:

<div id="my_placeholder" 
data-formurl="https://www.example.com/forms/embed.php?id=7777" 
data-formheight="1268"  
data-paddingbottom="10">
</div>
<script src="https://www.example.com/forms/js/jquery.min.js"></script>
<script src="https://www.example.com/forms/js/jquery.postmessage.min.js"></script>
<script src="https://www.example.com/forms/js/form_loader.js"></script>

Using that code it automatically puts the form in an iframe. 使用该代码,它会自动将表单放入iframe中。 The input field ID within the form i'd like to fill is #element_11 我要填写的表单中的输入字段ID为#element_11

So far, I have the following code which works exactly as I'd like but the generic form code is not in an iframe 到目前为止,我有以下代码可以完全按照我的意愿工作,但通用表单代码不在iframe中

<a href="#" onclick="reply_click(this)" data-filled-value="Text from clicking on link 1">Link1</a>
<br />
<a href="#" onclick="reply_click(this)" data-filled-value="Text from clicking on link 2">Link2</a>
<br />
<a href="#" onclick="reply_click(this)" data-filled-value="Text from clicking on link 3">Link3</a>

<form>

<input type="text" id="element_11" />

</form>

<script type="text/javascript">

function reply_click(element)
{
document.getElementById('element_11').value = element.getAttribute('data-filled-value');
}    

</script>

How can I accomplish this? 我该怎么做? I have searched and searched but every post I've come across wants to target the iframe and "submit" the form or "close" the iframe or "fill in one form and have it go to another form in an iframe". 我进行了搜索,但是遇到的每个帖子都希望以iframe为目标并“提交”表单或“关闭” iframe或“填写一种表单,然后将其转到iframe中的另一表单”。

Use .contents() to access the contents of an iframe. 使用.contents()访问iframe的内容。

function reply_click(element)
{
    $("#iframeID").contents().find("#element_11").val(element.dataset.filledValue);
}

Replace iframeID with the actual ID of the iframe. iframeID替换为iframe的实际ID。

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

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