简体   繁体   English

如何从jquery插件返回iframe内容?

[英]How to return Iframe content from a jquery plugin?

I want to write a jquery plugin which can submit form using iframe and return the content of iframe as the argument of callback function. 我想写一个可以使用iframe提交表单并返回iframe内容作为回调函数参数的jquery插件。 But onload event does not return any value. 但是onload事件不会返回任何值。 I am a beginner. 我是初学者。 Can you help? 你能帮我吗?

Recommendation 建议

The only situation I can imagine in which you will need to submit a form within an iFrame is if you want to do AJAX uploads, if that's your situation you can take a look at this plugin which implements this very elegantly (and they use an iFrame to submit the file upload) 我可以想象的唯一情况是,如果要进行AJAX上传,则需要在iFrame中提交表单;如果是这种情况,您可以看一下非常优雅地实现此功能的插件 (他们使用了iFrame)提交文件上传)

But if you simply want to submit a form and retrieve the resulting data while staying on the same page, its best not to use an iframe and stick with $.post() 但是,如果您只是想提交表单并在停留在同一页面上的同时检索结果数据,则最好不要使用iframe并坚持使用$ .post()

Example: 例:

$.post('ajax/test.html', form.serializeArray(), function(data) { // Callback function
    $('.result').html(data); // Do what you want with data
}, 'html'); // Retrieve HTML, json etc

If you still need it 如果您仍然需要

If you still need to use an iframe, below is the relevant code from the upload.js plugin: 如果您仍然需要使用iframe,则以下是upload.js插件中的相关代码:

     form.submit(function() { // form would be the form element in the iframe
        iframe.load(function() {
            var data = handleData(this);
                // data will be the resulting content
        });
     }).submit(); // submit form after applying listeners

And altering their handleData() function: 并更改其handleData()函数:

function handleData(iframe) {
            return $(iframe).contents().get(0);
}

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

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