【问题标题】:Bootstrap 4 / jQuery - Clear filename in input field <input type='file' /> bs-custom-file-inputBootstrap 4 / jQuery - 清除输入字段中的文件名 <input type='file' /> bs-custom-file-input
【发布时间】:2020-06-04 19:51:12
【问题描述】:

我得到了一个带有推荐插件的 Bootstrap 表单,用于为自定义文件输入设置动画:bs-custom-file-input。见:https://getbootstrap.com/docs/4.5/components/forms/#file-browser 该表单包含一个用于添加附件的输入字段 (type="file")。 如果用户选择了一个文件大小超过 1MB 的文件,则会显示一个带有错误消息的警告框。

如何清除错误信息后输入字段中的文件名?

到目前为止,这是我的代码:

HTML 表单

<div class="container">
<div class="row">
    <div class="col-md-12">

        <form id="testform" method="post" enctype="multipart/form-data">

            <div class="form-row">

                <div class="form-group col-12">
                    <label for="customFile">Attachment</label>

                    <div class="custom-file">
                        <input type="file" class="custom-file-input" id="customFile">
                        <label class="custom-file-label" for="customFile" data-browse="Browse">Select a file</label>
                    </div>
                </div>

            </div>

        </form>

    </div>
</div>

$(document).ready(function() {

$('#customFile').on('change', function() {

    // The recommended plugin to animate custom file input: bs-custom-file-input, is what bootstrap using currently
    bsCustomFileInput.init();

    // Set maximum filesize
    var maxSizeMb = 1;

    // Get the file by using JQuery's selector
    var file = $('#customFile')[0].files[0];

    // Make sure that a file has been selected before attempting to get its size.
    if(file !== undefined) {

        // Get the filesize
        var totalSize = file.size;

        // Convert bytes into MB
        var totalSizeMb = totalSize  / Math.pow(1024,2);

        // Check to see if it is too large.
        if(totalSizeMb > maxSizeMb) {

            // Create an error message
            var errorMsg = 'File too large. Maximum file size is ' + maxSizeMb + ' MB. Selected file is ' + totalSizeMb.toFixed(2) + ' MB';

            // Show the error
            alert(errorMsg);

            // Clear the value
            $('#customFile').val('');

            // How to clear the filename in the input field?
            alert('How to clear the filename in the input field?');

            // Return FALSE
            return false;
        }
    }

});

});

编辑:工作小提琴: https://jsfiddle.net/Matti79/racv6w4d/15/

我在这里发现了一个问题:https://github.com/Johann-S/bs-custom-file-input/issues/37,但我无法让它工作。

【问题讨论】:

  • 能不能用JavaScript去掉输入框,然后替换?
  • 不知道该怎么做,你能试试吗?

标签: javascript jquery


【解决方案1】:

没有要清除的值,插件为自定义文件生成一个标签,你看到的不是输入而是标签,如果你使用浏览器检查你可以看到它,替换这个:

// Clear the value
$('#customFile').val('');

通过这个:

// Clear the value, replace it by whatever text you want
$('#customFile').next('label').html('Select a file');

【讨论】:

  • 这似乎不起作用(仅在选择 1MB 以上的文件时)。当我第一次选择 1MB 以下的文件并更改为 1MB 以上的文件时,标签不会改变。我已经更新了小提琴:jsfiddle.net/Matti79/racv6w4d/23,所以你也可以自己尝试一下。
  • 这不起作用,因为您在更改时重新初始化bs-custom-file,您只需初始化一次。只需将bsCustomFileInput.init();放在$(document).ready之后即可
  • 成功了。谢谢网络直播!更新了 Fiddle 以获得一个工作示例。
【解决方案2】:

这适用于单个字段。它支持带有子标签(用于长名称或多个文件)

function remove_selected(id_input){
   var fileInput = document.getElementById(id_input)
   fileInput.value = ''
   fileInput.dispatchEvent(new Event('change'));
}

remove_selected('file');

来源:https://github.com/Johann-S/bs-custom-file-input/issues/86

【讨论】:

  • 唯一对我有用的解决方案,Vanilla 万岁 :)
  • 如果您添加此方法的原因会很棒。原因:bs-custom-fileinput 订阅了输入的 'change' 事件,当您以编程方式更改值时,不会触发此事件。所以我们不得不手动触发它。这就是 JS 的工作原理;与 Winforms/WPF/WinUI 中的 C# 不同。
【解决方案3】:

尝试复制 HTML:

// At beginning
var div = $('.custom-file')[0]; // Or use an ID
var html = div.innerHTML;

// To clear the input
div.innerHTML = html;

它只记住空输入字段的样子,并相应地更新代码。

请记住,div 中的所有内容都已重置。为了防止这种情况,请在输入周围添加一个额外的容器:

<div class="custom-file">
    <span id="customFileInputContainer">
        <input type="file" class="custom-file-input" id="customFile">
    </span>
    <label class="custom-file-label" for="customFile" data-browse="Browse">Select a file</label>
</div>

JavaScript 需要更改以适应这种情况:

var div = $('#customFileInputContainer')[0];

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多