简体   繁体   中英

text box doesn't update when I push the button

I have example here: code example .

HTML code:

<input id="uploadFile" placeholder="Choose File" disabled="disabled" />
<div class="file-upload btn btn-primary">
    <span>Upload</span> 
    <br> 
    <input id="uploadBtn" class="upload" type="file">
</div>

CSS code:

.btn {
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    display: inline-block;
    font-size: 14px;
    font-weight: normal;
    line-height: 1.42857;
    margin-bottom: 0;
    padding: 6px 12px;
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
}

.file-upload input.upload {
    cursor: pointer;
    font-size: 20px;
    margin: 0;
    opacity: 0;
    padding: 0;
    position: absolute;
    right: 0;
    top: 0;
}

.file-upload {
    margin-left: 10px;
    overflow: hidden;
    position: relative;
}
.btn-primary {
    background-color: #428bca;
    border-color: #357ebd;
    color: #fff;
}

JavaScript code:

document.getElementById("uploadBtn").onchange = function () {
    document.getElementById("uploadFile").value = this.value;
};

In JSFiddle site, when I push the upload button, the text box are updated, but when I copy the code to files, and run it under Firefox Mozilla, the text box doesn't update.

I take reference from this site: How to Style a HTML file upload button in Pure CSS

Any suggestions?

Your JS does not work since the uploadBtn is not created when you are trying to add the onchange listener. Try waiting for the window to load.

window.onload = function() {
   document.getElementById("uploadBtn").onchange = function () {
      document.getElementById("uploadFile").value = this.value;
   };
};

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