php - 使用输入字段(类型文件)选择图像后更改文本

标签 php html css

我这里有代码:

.upload-button{
    text-align: center;
    position:relative;
    width: 402px;
    word-wrap: break-word;
    border: 2px solid #037CA9;
    background-color: #131214;
    color: white;
    padding: 15px 0;
    display: inline-block;
    margin-bottom: 15px;
}

.upload-button:hover{
    text-decoration: underline;
}

input[type="file"]{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    opacity:0;
    cursor:pointer;
}
<div class="upload-button">
        Bild auswählen
        <input type="file" name="fileToUpload" id="fileToUpload">
</div>

现在我想更改框内的文本,如果有人选择了图像。我想看文件名。我怎么能意识到这样的事情?我需要 Javascript 吗?

最佳答案

您可以使用 this.files[0] 变量:

$("document").ready(function(){
    $('#fileToUpload').on('change', function(e){
        var file = this.files[0];
        $("#output").html('last modified: ' + file.lastModifiedDate + '<br>Name: '+file.name+'<br>Size: '+file.size+'<br>Type: '+file.type);
        $('.upload-button').html('Upload ' + file.name);
    });
});
.upload-button{
    text-align: center;
    position:relative;
    width: 402px;
    word-wrap: break-word;
    border: 2px solid #037CA9;
    background-color: #131214;
    color: white;
    padding: 15px 0;
    display: inline-block;
    margin-bottom: 15px;
}

.upload-button:hover{
    text-decoration: underline;
}

input[type="file"]{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    opacity:0;
    cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="upload-button">
        Bild auswählen
        <input type="file" name="fileToUpload" id="fileToUpload">
</div>
    
    <div id="output">
        
    </div>

关于php - 使用输入字段(类型文件)选择图像后更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189975/

相关文章:

php - 将值添加到数据透视表 Laravel

javascript - 用AJAX改变页面内容,然后执行JS

javascript - 倒数计时器css分析

html - 导航菜单未显示 : inline block

html - 双背景,一个背景从中间重复 - css3

php - 无法在 php 函数中运行数据库查询

php - 你如何实现一个好的亵渎过滤器?

javascript - ajax 调用后 php echo javascript 函数不起作用?

html - 找到3个不同高度的html div,第一个在左边,第二个和第三个在右边

jquery - 如何使用 hover droppable 函数获取元素的 id