简体   繁体   English

在iframe中访问HTML表单元素,但不包含ID /名称

[英]Access html form element, inside iframe without id/name

I have the following code-structure (nested HTMLs, although that probably doesn't make much of a difference) 我有以下代码结构(嵌套的HTML,尽管可能没有多大区别)

<html>
    <body>
        <iframe>
            <html>
                <form id="loginForm" name ="loginFormName">
                    <input id ="name" name ="username">
                    </input>
                </form>
            </html>
        </iframe>
    </body>
</html

I tried it by accessing the input field via: 我尝试通过以下方式访问输入字段:

document.getElementById("name");
document.loginFormName.username;

But I believe the iFrame hinders me to access the elements. 但是我相信iFrame会阻止我访问元素。 However, somehow accessing the iFrame first: 但是,以某种方式首先访问iFrame:

document.getElementByTagName("iframe).getElementById("name");

does not yield any results other than "undefined". 除了“未定义”以外,不会产生任何其他结果。

Help is much appreciated, I would like to auto-set the value of the input field and auto-submit the form. 非常感谢您的帮助,我想自动设置输入字段的值并自动提交表单。 (form.submit). (form.submit)。 I have no preference regarding Javascript or JQuery. 我对Javascript或JQuery没有偏好。

You can use .contents() for that. 您可以使用.contents()

The contents( ) method finds all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe. 如果元素是iframe,则content()方法将查找匹配的元素(包括文本节点)或内容文档中的所有子节点。

$('iframe').contents().find('input')

If you want to submit the form, you can use this. 如果要提交表单,可以使用此表单。

$('iframe').contents().find('form').submit();

Sample Fiddle demo to show how content in an iframe is accessed by contents( ) . 样本小提琴演示 ,展示了content contents( )如何访问iframe中的contents( )

Accessing an element of an iframe is very easy. 访问iframe的元素非常容易。

You should use the following syntax for access input element & form submit: 您应该对访问输入元素和表单提交使用以下语法:

$('iframe').contents().find('input'); //Form Element access

$('iframe').contents().find('#loginForm').submit(); //Form submit code

Hope it helps to resolve your query! 希望它有助于解决您的查询!

If your iframe is in the same domain as your parent page you can access the elements using document.frames collection. 如果您的iframe与父页面位于同一域中,则可以使用document.frames集合访问元素。

give any id to your iFrame and replace myIFrame with your iFrame id 给您的iFrame提供任何ID,并用您的iFrame ID替换myIFrame

window.frames['myIFrame'].document.getElementById('name') window.frames ['myIFrame']。document.getElementById('name')

If your iframe is not in the same domain the browser should prevent such access for security reasons. 如果您的iframe不在同一域中,则出于安全原因,浏览器应阻止此类访问。

The problem is you are loading your iframe incorrectly. 问题是您未正确加载iframe。

  <div class="main"> <iframe src="iframecontent.html"> </iframe> </div> <!--iframecontent.html :--> <html> <body> <form id="loginForm" name ="loginFormName"> <input id ="name" name ="username"> </input> </form> </body> </html> 

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

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