gpt4 book ai didi

jquery - 将文件输入标签文本更改为文件名

转载 作者:行者123 更新时间:2023-11-27 23:05:47 24 4
gpt4 key购买 nike

我正在尝试将文件输入字段的标签文本更改为文件名,但是我在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'files' of undefined

这是 HTML 和 JQuery:

<div class="custom-file">
<label class="custom-file-label text-left" for="customFile" id="file">Choose file</label>
<input type="file" class="custom-file-input" id="customFile">
</div>


<script type="text/javascript">
$('#customFile').change(function() {
var i = $(this).prev('label').clone();
var file = $('customFile')[0].files[0].name;
$(this).prev('label').text(file);
});
</script>

我还在学习 JQuery,所以非常感谢任何帮助!

最佳答案

我想你只是忘记了选择器上的 # for customFile

 var file = $('#customFile')[0].files[0].name;

这里是完整代码

 $('#customFile').on("change",function() {
console.log("change fire");
var i = $(this).prev('label').clone();
var file = $('#customFile')[0].files[0].name;
console.log(file);
$(this).prev('label').text(file);

});

https://codepen.io/jehadja/pen/pLrYwq?editors=1111

关于jquery - 将文件输入标签文本更改为文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49471597/

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