Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript验证<;输入类型=文件多个>;如果浏览器不支持多个_Javascript_File_Validation_Input - Fatal编程技术网

如何使用javascript验证<;输入类型=文件多个>;如果浏览器不支持多个

如何使用javascript验证<;输入类型=文件多个>;如果浏览器不支持多个,javascript,file,validation,input,Javascript,File,Validation,Input,我正在使用: <input type='file' name='pac1[]' multiple> 如何验证至少选择了一个文件 使用时: <input type='file' name='pac1'> 验证单个非数组项没有问题。您可以使用: <input id="upload" type='file' name='pac1[]' multiple> 和JS: if(document.getElementById("upload").files

我正在使用:

<input type='file' name='pac1[]' multiple>

如何验证至少选择了一个文件

使用时:

<input type='file' name='pac1'>

验证单个非数组项没有问题。

您可以使用:

<input id="upload" type='file' name='pac1[]' multiple>

和JS:

if(document.getElementById("upload").files.length < 1)
{
   alert("No file selected");
   return false;
}
if(document.getElementById(“upload”).files.length<1)
{
警报(“未选择文件”);
返回false;
}
您可以使用:

<input id="upload" type='file' name='pac1[]' multiple>

和JS:

if(document.getElementById("upload").files.length < 1)
{
   alert("No file selected");
   return false;
}
if(document.getElementById(“upload”).files.length<1)
{
警报(“未选择文件”);
返回false;
}

Bind
change
使用
addEventListener

INPUTElement.files.length
将返回所选文件的数量

document.querySelector('[name=“pac1[]”]).addEventListener('change',function(){
警报(this.files.length);
});

使用
addEventListener

INPUTElement.files.length
将返回所选文件的数量

document.querySelector('[name=“pac1[]”]).addEventListener('change',function(){
警报(this.files.length);
});

this.files.length
this.files.length
?这很有效。谢谢。发现了一个问题。如果浏览器(如IE8)不支持多个浏览器,该怎么办?然后脚本的长度检查失败。除了检查浏览器是否支持之外,还有其他解决方案吗?这很有效。谢谢。发现了一个问题。如果浏览器(如IE8)不支持多个浏览器,该怎么办?然后脚本的长度检查失败。除了检查浏览器是否支持之外,还有其他解决方案吗?如果没有选择文件,则不会触发更改事件:-)(“验证至少选择了一个文件”)@kaido,True!:P i没有考虑“验证”一词:(如果没有文件被选择,则更改事件不会触发:-)(“验证至少有一个文件被选中”)@ KAIIDO,真的!P I没有考虑“验证”一词: