【问题标题】:How to return Iframe content from a jquery plugin?如何从 jquery 插件返回 iframe 内容?
【发布时间】:2012-11-04 07:14:55
【问题描述】:

我想写一个 jquery 插件,它可以使用 iframe 提交表单并返回 iframe 的内容作为回调函数的参数。但是 onload 事件不返回任何值。我是初学者。你能帮我吗?

【问题讨论】:

  • iframe 内的表单? ...你的代码在哪里?你试过什么? ...您要一本食谱吗?

标签: jquery forms iframe plugins


【解决方案1】:

推荐

我能想象的唯一需要在 iFrame 中提交表单的情况是,如果您想进行 AJAX 上传,如果这是您的情况,您可以查看this plugin,它非常优雅地实现了这一点(并且他们使用 iFrame 提交文件上传)

但如果您只是想提交一个表单并在同一页面上检索结果数据,最好不要使用 iframe 并坚持使用$.post()

例子:

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

如果你还需要它

如果你仍然需要使用 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

并改变他们的handleData() 功能:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多