簡體   English   中英

如何從jquery插件返回iframe內容?

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

我想寫一個可以使用iframe提交表單並返回iframe內容作為回調函數參數的jquery插件。 但是onload事件不會返回任何值。 我是初學者。 你能幫我嗎?

建議

我可以想象的唯一情況是,如果要進行AJAX上傳,則需要在iFrame中提交表單;如果是這種情況,您可以看一下非常優雅地實現此功能的插件 (他們使用了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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM