简体   繁体   中英

Styling input type="file" - no "No file selected" appear

I've recently decided to try styling a input type="file" . The design works almost fine, the only problem is that I want the text "No file selected" appear in the filename selection. Even when I select a file no text does appear. I would appreciate any help and explanations!

I have the following code: HTML

<div class="fileuploader">
  <input type="text" class="filename" disabled="disabled" />
  <input type="button" name="file" class="filebutton" value="Browse" />
  <input type="file" name="avatar" />
</div>

and following CSS

.fileuploader {
    position: relative;
    display: inline-block;
    overflow: hidden;
    cursor: default;
}

.filename {
    float: left;
    display: inline-block;
    outline: 0 none;
    height: 30px;
    width: 302px;
    overflow: hidden;
    border: 1px solid #CCCCCC;
    color: #777;
    text-shadow: 1px 1px 0px #fff;
    border-radius: 5px 0px 0px 5px;
    box-shadow: 0px 0px 1px #fff inset;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    padding: 6px 10px;
}


    .filebutton {
        float: left;
        height: 30px;
        display: inline-block;
        outline: 0 none;
        cursor: pointer;
        border: 1px solid #CCCCCC;
        border-radius: 5px 5px 5px 5px;
        box-shadow: 0 0 1px #fff inset;
        color: #555555;
        margin-left: 3px;
        padding: 6px 12px;
        background: #DDDDDD;
        background:-moz-linear-gradient(top, #EEEEEE 0%, #DDDDDD 100%);
        background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #EEEEEE), color-stop(100%, #DDDDDD));filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#EEEEEE', endColorstr='#DDDDDD', GradientType=0);
    }

    .fileuploader input[type=file] {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        border: 0;
        height: 30px;
        cursor: pointer;
        filter:alpha(opacity=0);
        -moz-opacity: 0;
        -khtml-opacity: 0;
        opacity: 0;
        margin: 0;
        padding: 0;
    }

I want the text "No file selected" appear in the filename selection. Even when I select a file no text does appear.

One solution would be to update the filename whenever the file input fires a JavaScript 'change' event. This can easily be accomplished using jQuery's $.change() function:

$(document).ready(function() {
    $('input[type="file"]').change(function() {
        var val = ($(this).val()) ? $(this).val() : "No file selected.";
        $('.filename').attr('placeholder', val);
    });
});

http://api.jquery.com/change/

I've created a jsFiddle that includes this functionality: http://jsfiddle.net/cfY6m/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM