“文件上载”按钮中的Bootstrap 3到4迁移和输入大小问题

时间:2018-03-20 08:18:51

标签: html twitter-bootstrap-3 migration bootstrap-4

我一直在使用下面的代码片段。但是当我尝试将相同的代码移动到Bootstrap 4时,按钮大小和输入框大小没有对齐。

Bootstrap 3文件上传代码段

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<div class="col-md-10">
    <div class="input-group">
        <label class="input-group-btn">
            <span class="btn btn-primary">
                Choose File <input type="file" id="pdf-file" name="pdf_file" style="display: none;">
            </span>
        </label>
        <input type="text" class="form-control" id="pdf-name" placeholder="Select the PDF File" readonly>
    </div>
</div>

Bootstrap 4文件上传代码段

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css">
<div class="col-10">
    <div class="input-group">
        <label class="input-group-btn">
            <span class="btn btn-primary">
                Choose File <input type="file" id="pdf-file" name="pdf_file" style="display: none;">
            </span>
        </label>
        <input type="text" class="form-control" id="pdf-name" placeholder="Select the PDF File" readonly>
    </div>
</div>

请帮我将上述代码迁移到Bootstrap 4。

2 个答案:

答案 0 :(得分:0)

你这样试试

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

		$('.btn-file :file').on('fileselect', function(event, label) {
		    
		    var input = $(this).parents('.input-group').find(':text'),
		        log = label;
		    
		    if( input.length ) {
		        input.val(log);
		    } else {
		        if( log ) alert(log);
		    }
	    
		});
		function readURL(input) {
		    if (input.files && input.files[0]) {
		        var reader = new FileReader();
		        
		        reader.onload = function (e) {
		            $('#img-upload').attr('src', e.target.result);
		        }
		        
		        reader.readAsDataURL(input.files[0]);
		    }
		}

		$("#imgInp").change(function(){
		    readURL(this);
		}); 	
	});
.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;
    outline: none;
    background: white;
    cursor: inherit;
    display: block;
}

#img-upload{
    width: 100%;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<div class="container">
<div class="col-md-6">
    <div class="form-group">
        <label>Upload Image</label>
        <div class="input-group">
            <span class="input-group-btn">
                <span class="btn btn-default btn-file">
                    Browse… <input type="file" id="imgInp">
                </span>
            </span>
            <input type="text" class="form-control" readonly>
        </div>
        <img id='img-upload'/>
    </div>
</div>
</div>

答案 1 :(得分:0)

你可以先看here 另一方面,Bootstrap 4在文件输入方面有一些新的强大功能。您可以了解更多here