2012-01-29 83 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