【问题标题】:Insert Text into a Form in iFrame and click on Submit在 iFrame 中将文本插入表单,然后单击提交
【发布时间】:2022-04-15 01:15:01
【问题描述】:

好的。我基本上想在我的网站上创建一个 iframe,它可以通向我的其他网站。

在我的 iFramed 网站上,我有一个带有文本字段和提交按钮的表单。我想在文本字段中插入一些文本,然后在单击站点 A 上的按钮(包含 iframe 的那个)时单击提交按钮。

有没有可能?

我尝试了以下方法,但它似乎不起作用:

<script type="text/javascript">
    function insertTxt() {
        var textstring = "test successful";
        window.frames["iframeAB"].document.documentElement.getElementsByTagName("textarea")[0].value = textstring;
    }
</script>

<iframe name="iframeAB" src ="index.html" id="qab" width="100%" height="210" style="border:1px solid #009ee0">
    Your browser doesn't handle iframes
</iframe>
<input type="button" value="Go" onclick="top.frames['iframeAB'].window.document.body.getElementById('text_field').value='test successful';"/>
<a href="#" onclick="insertTxt();">test</a>

【问题讨论】:

  • 除非在同一个域、端口和协议上,否则不可能

标签: javascript jquery html iframe


【解决方案1】:

尝试使用window.frames["iframeAB"].documentContent 而不是window.frames["iframeAB"].document

【讨论】:

    【解决方案2】:

    使用 jQuery 试试这个:

    // Include the jQuery file inside the head tag
    // Download the latest version, this is in the jQuery website
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    
    <script>
    // Then call the code inside ready method like this
    $(document).ready(function () {
        // Get the iFrame jQuery Object
        var $MyFrame = $("iframe[name='iframeAB']");
    
        // You need to wait for the iFrame content to load first
        // So, that the click events work properly
        $MyFrame.load(function () {
            var frameBody = $MyFrame.contents().find('body');
    
            // Find the textarea by id
            var textarea = frameBody.find('#text_field');
    
            // Set the value here...
            textarea.val(textstring);
        });
    });
    
    </script>
    

    【讨论】:

    • 我不太擅长 jQuery。你能告诉我如何调用这个函数吗?或者我应该把这段代码粘贴到 Javascript 标签之间的头部,它会起作用吗?
    • 它应该适用于其他域吗?我的意思是站点 A 位于我的域中,站点 B(iFramed 站点)位于另一个域中。在那种情况下会起作用吗?
    • 它似乎根本不起作用:(我已经厌倦了创建索引文件并创建一个包含表单本身的文件,但它什么也没做。跨度>
    【解决方案3】:

    你可以试试这个解决方案:

    它使用 JQuery

    您的 HTML

    <iframe name="iframeAB" src ="index.html" id="qab" width="100%" height="210" style="border:1px solid #009ee0">
        Your browser doesn't handle iframes
    </iframe>
    <input type="button" value="Go" onclick="top.frames['iframeAB'].window.document.body.getElementById('text_field').value='test successful';"/>
    <a href="#" onclick="insertTxt();">test</a>
    

    在JS文件中插入Txt()方法

    $("#qab").ready(function() {
        $("#qab").contents().find("body").find('textarea').val('Test Successful');
    });
    

    【讨论】:

      【解决方案4】:

      这应该可以,试试就知道了

      <script>
          let preFillForm = () => {
      
              //name attribute of iframe in box bracket
              let iframe = window.frames["myiframe"].document
      
              iframe.getElementById("email").value = 'kdbagwe007@gmail.com'
              iframe.getElementById("password").value = 'kdbagwe007'
      
              //click button after 4 seconds of iframe load or whenever u want
              setTimeout(() => {
                  console.log('click')
                  iframe.getElementById("submit").click();
              }, 4000);
          }
      </script>
      
      <iframe name="myiframe" width="600px" height="400px" src="your-url"
          onload="preFillForm()"
      >
      </iframe>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        • 2018-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多