Upload file button Landscaping, upload files before and after state control

Source: Internet
Author: User

When we do input text upload, HTML comes with the upload button is ugly, how to beautify it? Similarly: input checkbox landscaping, input Radio beautification is a truth.

The beautification idea of the input File Upload button is to set the previous button transparency opacity to 0, and then wrap the outer layer with a div to achieve the beautification function.

Note: When the type of input is file, its name must have a value, because this name will be the array name for uploading the file information.

<href= "javascript:;" class = "A-upload mr10" ><  type= "file"  name= "myFiles"  ID= ""  > Click here to upload the file </a>
. A-upload{padding:4px 10px;/*height:34px;*/Line-height:28px;position:relative;cursor:Pointer;Color:#fff;Background-color:#286090;Border-color:#204d74;Border-radius:4px;Overflow:Hidden;Display:Inline-block;*display:inline;*zoom:1; }. A-upload input{position:Absolute;font-size:100px; Right:0;Top:0;Opacity:0;Filter:Alpha (opacity=0);cursor:Pointer}. A-upload:hover{Color:#FFFFFF;background:#337ab7;Border-color:#204d74;text-decoration:None; }

The above landscaping, the default selection of files, the displayed file name is also hidden, then how to display the selected file name? It doesn't matter, we can use jquery to get the file name.

We can write a change event.

<ahref= "javascript:;"ID= "Upload"class= "A-upload mr10"><inputtype= "File"name=""ID="">Click here to upload the file</a>//Add a div that displays the file name<Divclass= "Showfilename"></Div>
Upload button
<button id= "uploadbtn" type= "button" class= "Btn btn-primary" > Uploads </button>
$ (function() {

 Show hidden file name and upload status toggle
$ ('. Showfilename '). Hide ();
$ (' #uploadBtn '). Hide ();
$ ("#upload"). On ("Change", "input[type= ' file ']", function () {
var FilePath = $ (this). Val ();
  If only upload picture if (filepath.indexof ("jpg")! =-1 | | filepath.indexof ("PNG")! =-1) {
if (FilePath) {
$ (". Fileerrortip"). HTML (""). Hide ();
var arr = filepath.split (' \ \ ');
var fileName = arr[arr.length-1];
$ ('. Showfilename '). Show ();
$ (' #uploadBtn '). Show ();
$ (". Showfilename"). HTML ("FileName selected:" + filename);
$ (' #upload '). Hide ();
} else {
$ (". Showfilename"). HTML ("");
$ (". Fileerrortip"). HTML ("You did not upload the file, or you uploaded a file of the wrong type!") "). Show ();
return False
}
});

});

When a file is not selected

After selecting a file

Upload file button Landscaping, upload files before and after state control

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.