5

我一直在尝试设置输入 type="file" 字段的样式。我的按钮是样式化的,但我似乎无法弄清楚如何让文件路径/文件在用户选择要上传的文件时显示。有人可以帮忙吗?

<form action="" method="POST" enctype="multipart/form-data">
    <div class="fileupload-buttonbar">
        <label class="file-upload"><span>Upload....</span><input name="uploadfile" type="file"> </label>    
    </div>
</form>

CSS...

.file-upload {
    overflow: hidden;
    display: inline-block;
    position: relative;    
    vertical-align: middle;
    text-align: center;
    color: #fff;
    border: 2px solid #707070;
    background: #A0A0A0;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    border-radius: 8px;
    text-shadow: #000 1px 1px 4px;
    cursor:pointer;
}
.file-upload:hover {
    background: #2FA2FF;
}
.file-upload.focus {
    outline: 2px solid yellow;
}
.file-upload input {
    position: absolute;
    top: 0;
    left: 0;
    margin: 0;
    font-size: 12px;
    opacity: 0;
    filter: alpha(opacity=0);
    z-index:-1;
}
.file-upload span {
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;
    padding-top: .45em;
}
.file-upload { height: 38px; }
.file-upload,
.file-upload span { width: 160px; }    
.file-upload-status {
    margin-left: 10px;
    vertical-align: middle;
    padding: 7px 11px;
    font-weight: bold;    
    font-size: 16px;
    color: #888;
    background: #f8f8f8;
    border: 3px solid #ddd;
}

​</p>

我的代码在这里 http://jsfiddle.net/hjNEC/

4

2 回答 2

8

向输入字段添加一个change事件,然后获取它的.value.

示例(使用 jQuery):

$('input[name="uploadfile"]').change(function(){
    var fileName = $(this).val();
    alert(fileName);
});

演示:http: //jsfiddle.net/hjNEC/2/

编辑:由于输入字段是隐藏的,并且文件名是其中的一部分,因此您必须fileName自己在页面上显示。

于 2012-12-28T20:46:27.003 回答
1
<form action="" method="POST" enctype="multipart/form-data">
     <div class="fileupload-buttonbar">
          <input type="file" name="uploadfile" id="uploadfile" style="display:none" onchange="file_path_display.innerHTML=uploadfile.value"/>
          <span class="file-upload" onclick="uploadfile.click()" ondragdrop="uploadfile.dragdrop()">Upload....</span>
          <div id="file_path_display"></div>
     </div>
</form>
于 2014-11-30T15:44:34.640 回答