JavaScript getElementByName().value 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14921421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript getElementByName().value not working
提问by Abhijit Das
I want alert if no file is selected. But in this code When a file is selected. It Still show The alert. Please Tell me how to fix it.
如果没有选择文件,我想要警报。但是在此代码中选择文件时。它仍然显示警报。请告诉我如何解决它。
<script>
function null_upload()
{
var a = document.getElementsByName("upload_file").value;
if(a == null)
{
alert('Please Select Min 1 File.');
return false;
}
}
</script>
Upload Form is
上传表格是
<input type="file" name="upload_file">
<input type="image" src="img/upload.png" id="upload_botton" title="Upload Image" name="submit" onclick="return null_upload()"/>
回答by VisioN
Method getElementsByName
returns a collection (i.e. a set of multiple elements) instead of a single DOM element (as, for example, getElementById
does).
方法getElementsByName
返回一个集合(即一组多个元素)而不是单个 DOM 元素(例如getElementById
)。
To get the first matched element use the following:
要获得第一个匹配的元素,请使用以下命令:
var a = document.getElementsByName("upload_file")[0].value;