gpt4 book ai didi

jquery - Bootstrap 文件输入不显示动态添加的文件输入的文件名

转载 作者:搜寻专家 更新时间:2023-10-31 23:23:57 25 4
gpt4 key购买 nike

我正在使用 Bootstrap 文件输入。它不显示动态添加的文件输入的浏览文件名。这是代码,

HTML

<div class="container" style="margin-top: 20px;">
<div class="row">
<div class="col-lg-6 col-sm-6 col-12">
<div class="fileinputs">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Browse&hellip; <input type="file" multiple>
</span>
</span>
<input type="text" class="form-control" readonly>
</div><br/>
</div>
<a href="#" id="new-btn">Add New</a>
<div id="new-div"></div>
</div>
</div>

CSS

.btn-file {


position: relative;
overflow: hidden;
}
.btn-file input[type=file] {
position: absolute;
top: 0;
right: 0;
min-width: 100%;
min-height: 100%;
font-size: 100px;
text-align: right;
filter: alpha(opacity=0);
opacity: 0;
background: red;
cursor: inherit;
display: block;
}
input[readonly] {
background-color: white !important;
cursor: text !important;
}

JQUERY

$(document).ready( function() {
$('#new-btn').on('click', function(){
$('#new-div').append($('.fileinputs').html());
});

$('.btn-file :file').on('fileselect', function(event, numFiles, label) {

var input = $(this).parents('.input-group').find(':text'),
log = numFiles > 1 ? numFiles + ' files selected' : label;

if( input.length ) {
input.val(log);
} else {
if( log ) alert(log);
}

});
});

$(document).on('change', '.btn-file :file', function() {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});

通过使用此代码,我只能在第一个文件字段中看到文件名。对于动态添加的文件输入,它不起作用。

这是我的 jsfiddle .如何在相应位置显示浏览过的文件名?

最佳答案

您需要使用委托(delegate)事件处理程序来捕获您的 fileselect 事件,因为新文件输入在页面加载后动态附加到 DOM,原始处理程序已附加到 DOM 中。试试这个:

$(document).on('fileselect', '.btn-file :file', function(event, numFiles, label) {
// your code...
});

Updated fiddle

关于jquery - Bootstrap 文件输入不显示动态添加的文件输入的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738304/

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