gpt4 book ai didi

javascript - 如何使用 JavaScript 或 JQuery 从 input type=file html 元素获取文件名?

转载 作者:行者123 更新时间:2023-12-03 21:54:11 25 4
gpt4 key购买 nike

在 Google Chrome 浏览器中,我尝试了多种方法+以下方法,但没有一个方法给我附加的文件名的值,验证后我将提交文件。但总是未定义或 val() 未找到..

如何解决?

console.log($("input[name='attachment[]']"));
/* Output:
[
<input type=​"file" name=​"attachment[]​" id=​"attachment">​
,
<input type=​"file" name=​"attachment[]​" id=​"attachment">​
,
<input type=​"file" name=​"attachment[]​" id=​"attachment">​
]
*/

$.each($("input[name='attachment[]']"), function(i,v) {
console.log(i);
console.log(v); //v.val() does not exist... even uploaded a file and showing file

});

/* Output:
0
<input type=​"file" name=​"attachment[]​" id=​"attachment">​
1
<input type=​"file" name=​"attachment[]​" id=​"attachment">​
2
<input type=​"file" name=​"attachment[]​" id=​"attachment">​
*/

return false;

最佳答案

这应该有效:

$("input[name='attachment[]']").each(function() {
var fileName = $(this).val().split('/').pop().split('\\').pop();
console.log(fileName);
});

您无法获取文件的完整路径,因为它取决于您使用的浏览器。输入文件唯一常见的跨浏览器值是文件名。

关于javascript - 如何使用 JavaScript 或 JQuery 从 input type=file html 元素获取文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10683192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com