Google <input type= ' file ' > change only responds to 1 resolutions and changes in style

Source: Internet
Author: User

In the project process encountered the need to upload local files, file's original control is not very beautiful, but this control and button a little bit different,

The idea of changing this style is to set a layer of links outside the control, and the file control's transparency to 0 (transparent). the style only needs to operate on the outer layer.

HTML code:

<TD style= "Text-align:left;" >
<a href= "javascript:;" class= "File" > select files
<input type= "file" Name= "fi" id= "F" >
</a>
</td>

CSS code:

. File {
position:relative;
Display:inline-block;
Background:rgb (146, 174);
border:0px Solid RGB (146, 174);
border-radius:4px;
padding:4px 12px;
Overflow:hidden;
color: #fff;
Text-decoration:none;
text-indent:0;
line-height:20px;
}
. File Input {
Position:absolute;
font-size:100px;
right:0;
top:0;
opacity:0;
}

Since the click Select File Change event responds only once, that is, the same file name of the file or the same file after the modification does not refresh the page when you click Submit will not respond again.

If the Click event is used here, you will need to click Submit and then click Select File to take effect. Here I looked at a lot of ways, in the Google Browser, the value of the file control can only be set to NULL, otherwise it will be an error.

So the idea here is to set the value of file to null each time you click. after testing, this problem was solved.

JS Code:

function Choosefile () {
$ ("#f"). Live (' click ', Function (e) {
$ ("#f"). Val ("");
});//file is the change based on the Val in the space. Google Explorer can only set the file Val to empty, set to other values will be error, so here to add a click event
$ ("#f"). Live (' Change ', function (e) {
var selectedfile = document.getElementById ("f"). Files[0];
var name = Selectedfile.name;
$ ("#filename"). Val (name);
var reader = new FileReader ();
Reader.readastext (selectedfile, "GBK");
reader.onload = function () {
$ ("#script_content"). Val (this.result);
$ ("#script_result"). Val ("");
}
});
}

This is my use of the process of some of the solutions, if there are errors please light spray, thank you!

Google <input type= ' file ' > change only responds to 1 resolutions and changes in style

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.