Javascript 无法从输入标记中选择文件数组值
我正在尝试查找用户是否上传了任何图像 下面是代码Javascript 无法从输入标记中选择文件数组值,javascript,jquery,Javascript,Jquery,我正在尝试查找用户是否上传了任何图像 下面是代码 <input type="file" name="profile_gallery[]"> <div id='submiform'>submit</div> 错误是 alert($('input[name=profile_gallery]').val()); //show undefined after and before selecting the file alert(profile
<input type="file" name="profile_gallery[]">
<div id='submiform'>submit</div>
错误是
alert($('input[name=profile_gallery]').val()); //show undefined after and before selecting the file
alert(profile_gallery.length); //Uncaught TypeError: Cannot read property 'files' of undefined
第二个
$(function(){
$('#submitform').on('click', function(){
var profile_gallery = ($('input[name=profile_gallery]'))[0].files;
alert(profile_gallery.length);
});
});
错误是
alert($('input[name=profile_gallery]').val()); //show undefined after and before selecting the file
alert(profile_gallery.length); //Uncaught TypeError: Cannot read property 'files' of undefined
这可以通过使用
有几个选择
- File.name:返回文件对象引用的文件的名称
- File.size:返回文件的大小李>
- File.type:返回文件的MIME类型
- File.name:返回文件对象引用的文件的名称
- File.size:返回文件的大小李>
- File.type:返回文件的MIME类型
因此,输入[name^=profile\u gallery]是获取它的一个可能的选择器。但是你为什么需要这样一个名字呢?@raina77ow brilliant bro感谢它奏效了,但是你能解释一下它为什么奏效吗?@vSugumar给你:
$('input[name=profile\u gallery]')
不起作用,因为“profile\u gallery”
不等于“profile\u gallery[]”$('input[name^=profile\u gallery]')
之所以有效,是因为“profile\u gallery”
和“profile\u gallery[]”
以profile\u gallery
开头的输入[name^=profile\u gallery]
都是获取它的一个可能的选择器。但是你为什么需要这样一个名字呢?@raina77ow brilliant bro感谢它奏效了,但是你能解释一下它为什么奏效吗?@vSugumar给你:$('input[name=profile\u gallery]')
不起作用,因为“profile\u gallery”
不等于“profile\u gallery[]”$('input[name^=profile\u gallery]')
之所以有效,是因为“profile\u gallery”
和“profile\u gallery[]”
都以profile\u gallery
开头。