Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在iFrame中将文本插入表单,然后单击Submit_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript 在iFrame中将文本插入表单,然后单击Submit

Javascript 在iFrame中将文本插入表单,然后单击Submit,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,好的。我基本上想在我的网站上创建一个iframe,它可以连接到我的其他网站 在我的iFramed网站上,我有一个带有文本字段和提交按钮的表单。我想在文本字段中插入一些文本,然后在单击站点a的按钮(包含iframe的按钮)时单击submit按钮 有可能吗 我尝试过以下方法,但似乎不起作用: <script type="text/javascript"> function insertTxt() { var textstring = "test successfu

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

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

有可能吗

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

<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>

函数insertTxt(){
var textstring=“测试成功”;
window.frames[“iframeAB”].document.documentElement.getElementsByTagName(“textarea”)[0]。值=文本字符串;
}
您的浏览器无法处理iFrame

尝试使用
window.frames[“iframeAB”]。documentContent
而不是
window.frames[“iframeAB”]。document
尝试使用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>
//在head标记中包含jQuery文件
//下载最新版本,这在jQuery网站上
//然后像这样调用ready方法中的代码
$(文档).ready(函数(){
//获取iframejQuery对象
var$MyFrame=$(“iframe[name='iframeAB']);
//您需要先等待iFrame内容加载
//因此,单击事件可以正常工作
$MyFrame.load(函数(){
var frameBody=$MyFrame.contents().find('body');
//按id查找textarea
var textarea=frameBody.find('#text_field');
//在此处设置值。。。
textarea.val(textstring);
});
});

您可以尝试以下解决方案:

它使用JQuery

你的HTML


我不太擅长jQuery。你能告诉我如何调用这个函数吗?或者我应该把这段代码粘贴到Javascript标记之间的头上,它就可以工作了?它甚至可以用于其他域?我的意思是站点A在我的域中,站点B(iFramed)在另一个域中。在这种情况下,它会起作用吗?它似乎根本不起作用:(我已经厌倦了创建一个索引文件,并创建一个包含表单本身的文件,但它似乎没有任何作用。除非在相同的域、端口和协议上,否则不可能
<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>
$("#qab").ready(function() {
    $("#qab").contents().find("body").find('textarea').val('Test Successful');
});