2012-01-29 89 views
0

我有一堆複選框,鏈接到用戶可以下載的文檔。我在這裏攻擊了一些代碼。基於複選框的文件下載

到目前爲止,downloadChecked函數按預期工作,但我似乎無法使makFrame函數正常運行。目前它似乎沒有做任何事情。

function makeFrame(url) 
{ 
ifrm = document.createElement("IFRAME"); 
ifrm.setAttribute("style", "display:none;") ; 
ifrm.setAttribute("src", url) ; 
ifrm.style.width = 0+"px"; 
ifrm.style.height = 0+"px"; 
document.body.appendChild(ifrm) ; 
} 

function downloadChecked() 
{ 
for(i = 0 ; i < document.downloadform.elements.length ; i++) 
{ 
    foo = document.downloadform.elements[ i ] ; 
    if(foo.type == "checkbox" && foo.checked == true) 
    { 
    makeFrame('somefile.do?command=download&fileid=' + foo.name); 
    } 
} 
} 

和相應的HTML

<form name="downloadform"> 
<input type="checkbox" name="file" id="file1"  
value="file source etc" /> 

<input type="checkbox" name="file" id="file2"  
value="file source etc" /> 

<input type="button" value="Download all" onClick="downloadChecked();" /> 
+0

檢查開發人員/調試工具是否有任何錯誤。這將幫助我們確定你的功能在哪一點失敗。如果您使用Firefox,則需要使用[Firebug](http://getfirebug.com/)。所有最近的市長瀏覽器,包括IE9 +都內置了開發者工具。 – Kiruse 2012-01-29 23:12:47

+0

你的下載處理程序「somefile.do」是否爲你的文件設置了適當的頭文件? – Michal 2012-01-30 00:01:47

回答

0

在聲明

ifrm = document.createElement("IFRAME"); 

裏面的功能,要創建一個全局對象。因此,每次調用makeframe()時,都會破壞以前的iframe。 嘗試:

var ifrm = document.createElement("IFRAME"); 
+0

感謝您的迴應,但它似乎仍然不工作,雖然 – 2012-01-29 22:24:49

+0

究竟是什麼問題?沒有iframe被加載?只有一個? – 2012-01-29 22:25:28

+0

當我按下下載按鈕時,它什麼都不做。資源管理器似乎加載一瞬間然後停止。沒有下載提示要麼一個或多個選擇 – 2012-01-29 22:28:21

0

您的iframe代碼是正確的,應該工作。然而,在您的downloadChecked函數中,您將foo.name附加爲您的fileid查詢字符串屬性,該屬性使用您當前的標記將導致「& fileid = file」。假設您在輸入的值屬性中存儲了您的查詢字符串的fileid,您可能需要將「foo.name」更改爲「foo.value」

+0

是的,你是正確的,而我現在已經改變它.value,我仍然沒有得到任何下載提示/消息,似乎沒有任何事情發生 – 2012-01-29 23:36:28