2012-04-05 24 views
1
$(function() { 
    $(".preview").click(function() { 
     $('#image').wrap('<form action="/index/upload.php" method="post" enctype="multipart/form-data" id="imageform" target="imageupload" />'); 
     $('#imageform').submit(); 
     $('#image').unwrap(); 
     var image = $('#imageupload').filename; 
     return false; 
    }); 
}); 

<iframe style="display: none;" name="imageupload" id="imageupload"></iframe> 
<input type="file" id="image" name="image"> 
<input type="button" value="Preview" class="preview"> 

在圖像後的上述代碼是提交給它創建在iframe可變我檢索包含載的圖像的文件名的服務器。我的問題是,檢索文件名時,jQuery的.submit()函數沒有任何回調時表單是提交因此,當圖像被上傳我的代碼試圖獲得的文件名時,它現在還沒有。有沒有人知道我可以解決這個問題的方法?表單提交後,從iframe獲取變量?

+0

這是您的實際代碼?它看起來像圖像的輸入是在iframe之外,而不是在它之內。請務必發佈真實的代碼,以便我們不花時間排除一個不真實的問題。 :) – jmort253 2012-04-05 07:48:26

+0

這是我真正的代碼,圖像正在提交給iframe進行處理。 iframe將文件保存在服務器上,並在包含文件名的iframe中創建一個變量。我需要從iframe中檢索文件名。 – Renari 2012-04-05 07:53:31

+0

您確定目標屬性將定位到iframe嗎?我今天剛剛讀到,任何名稱被傳遞到「目標」被用作新窗口的標題,並且任何新鏈接被點擊將替換該目標。它沒有提到定位內聯框架。也許你知道我不知道的事情,但你應該仔細檢查,以防萬一:)祝你好運! – jmort253 2012-04-05 08:40:17

回答

1

我發現,I幀本身具有onload事件這樣我可以提交的iframe後觸發這和代碼繼續上載文件後根據需要。

$(function() { 
    $(".preview").click(function() { 
     $('#image').wrap('<form action="/index/upload.php" method="post" enctype="multipart/form-data" id="imageform" target="imageupload" />'); 
     $('#imageupload').attr('onload', 'preview()'); 
     $('#imageform').submit(); 
     $('#image').unwrap(); 
     return false; 
    }); 
}); 
function preview() { 
    var image = $('#imageupload').contents().find('#filename').html(); 
} 


<iframe style="display: none;" onload="" name="imageupload" id="imageupload"></iframe> 
<input type="file" id="image" name="image"> 
<input type="button" value="Preview" class="preview"> 
+0

+1 - 解決你自己的問題的好工作!您可以通過點擊答案左側的複選框將其標記爲已接受/最佳答案。這將作爲對具有相同問題的其他人的參考。 :) – jmort253 2012-04-06 05:33:20

+1

會做,但因爲我有小於100的聲譽,我不能記住我自己的崗位作爲答案24小時。 – Renari 2012-04-06 05:41:46

+0

哦,對。忘了那個。那麼,你有時間去找一些你可以回答的問題,這樣你就可以超過100個門檻。 :) – jmort253 2012-04-06 05:43:57