【问题标题】:Why disabled attribute does not removed in modal?为什么禁用属性不会在模态中删除?
【发布时间】:2019-11-10 12:55:53
【问题描述】:

我有模式,用户可以上传他们的文件。默认情况下,上传按钮被禁用。因此,当用户有插入文件时,应该启用该按钮。但这不会发生。请任何人帮忙。

我也尝试了以下两种方法,但没有帮助。

HTML

<div class="modal-header">
    <h4 class="modal-title text-orange2">Upload ES Value File</h4>
    <button type="button" class="close" data-dismiss="modal">&times;</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");
    }
});

【问题讨论】:

  • 试试$("#btnUpload").attr('disabled', false);
  • 没有变化

标签: jquery html bootstrap-4 bootstrap-modal


【解决方案1】:

尝试使用 prop()

function toggleUploadButton() {
    $("#btnUpload").prop('disabled', $("#esvaluefile").val() == "");
}
$("#esvaluefile").change(toggleUploadButton)
    .on('show.bs.modal', toggleUploadButton);

【讨论】:

    猜你喜欢
    • 2014-06-20
    • 2014-04-11
    • 1970-01-01
    • 2017-12-24
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    相关资源
    最近更新 更多