2013-04-20 49 views
0

我想要做类似的事情, 当用户上传一个文件后,我想让这个文件上传器禁用。 换句话说,一旦文件被升级,用户将不允许上传另一个文件。如何禁用文件上传?

我已经试过

<script type="text/javascript"> 
document.getElementById(file1).disabled = true; 
</script> 

<input type="file" name="submission_file" id="file1"> 

但它不工作。 我该怎么做?

+0

也正在做服务器端检查?我可以轻松关闭JavaScript并解决此问题。 – Springie 2013-04-20 09:03:38

回答

2

您还没有放置单个qoutes的ID

document.getElementById('file1').disabled = true; 

详细的解答

<script> 
function diableField() 
{ 
    document.getElementById('file1').disabled = true; 
} 
window.onload=diableField; 

</script> 
+0

仍然没有工作。它显示错误未捕获TypeError:无法设置属性'禁用'null。为什么? – 2013-04-20 08:09:53

+0

可能是在脚本加载到页面之前,它正在尝试禁用文件字段。可以放在你的文件字段加载后,并更好地封装在window.load函数中,以确保你的html加载正确。 – 2013-04-20 09:45:20

+0

@NiraliJoshi检查我的更新详细的答案。它可能会帮助你。 – 2013-04-20 09:51:00

0

您没有正确引用元素ID。试试这个:

document.getElementById('file1').disabled = true; 
0

尝试这个 -

<script type="text/javascript"> 
function disableThis(){ 
document.getElementById("file1").disabled = true; 
} 
</script> 

<input type="file" name="submission_file" id="file1" onchange="disableThis()"> 
0

请使用此代码,我也面临着这种类型的问题,这个代码很有用d对我来说。希望如此它也为你工作..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>File Upload</title> 
</head> 
<body> 
<script type="text/javascript"> 

function disableFileUpload(){ 

document.getElementById("file1").disabled = true; 

} 
</script> 
<input type="file" name="submission_file" id="file1" onchange="disableFileUpload()"> 
</body> 
</html> 

谢谢...