我有模式,用户可以在其中上传文件。默认情况下,上载按钮处于禁用状态。因此,当用户插入文件时,应启用该按钮。但这不会发生。任何人都可以帮忙。
我还尝试了以下2种方法,但没有帮助。
HTML
<div class="modal-header">
<h4 class="modal-title text-orange2">Upload ES Value File</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<form name="esvalueForm" id="esvalueForm" action="" method="POST" enctype="multipart/form-data">
<div class="modal-body">
<div class="pd-20 bg-white border-radius-4">
<div class="row">
<div class="col-lg-12 col-sm-12">
<div class="custom-file">
<input type="file" name="upload_file" id="esvaluefile" class="import-file" accept=".csv">
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="btnUpload" class="btn btn-primary" data-toggle="modal" data-target="#confirmation-upload" disabled>UPLOAD</button>
</div>
</div>
</div>
</form>
JS(2种方法)
$("#esvaluefile").change(function() {
if ($("#esvaluefile").val() == "") {
$("#btnUpload").attr('disabled', true);
}
else {
$("#btnUpload").removeAttr("disabled");
}
});
$("#esvaluefile").on('show.bs.modal', function (e) {
if ($("#esvaluefile").val() == "") {
$("#btnUpload").attr('disabled', true);
}
else {
$("#btnUpload").removeAttr("disabled");
}
});
答案 0 :(得分:0)
尝试使用prop()
function toggleUploadButton() {
$("#btnUpload").prop('disabled', $("#esvaluefile").val() == "");
}
$("#esvaluefile").change(toggleUploadButton)
.on('show.bs.modal', toggleUploadButton);