input[tyle= "file"] style modification and upload file name display

Source: Internet
Author: User

Default upload style We always feel not very good, according to the needs of the total want to change to and the structure of the unified style ...

Implementation methods and ideas: 1. In the input element plus a hyperlink label 2. Set the button style for the A tag 3. Set input[type= ' file ' to be transparent and positioned to cover on a

HTML code:

<class= "Input-file input-fileup"  href= "javascript:;" >      + Select File <size= " +" type= "File" name = "File" ID = "File" > </ a >

CSS code:

. Input-file{Display:Inline-block;position:relative;Overflow:Hidden;text-align:Center;width:Auto;Background-color:#2c7;Border:Solid 1px #ddd;Border-radius:4px;padding:5px 10px;font-size:12px;Font-weight:Normal;Line-height:18px;Color:#fff;text-decoration:None;}. Input-file input[type= "file"]{position:Absolute;Top:0; Right:0;font-size:14px;Background-color:#fff;Transform:Translate ( -300px, 0px) scale (4);Height:40px;Opacity:0;Filter:Alpha (opacity=0); }

Effect:

The uploaded file name can not be displayed at this time, if you want to display the uploaded filename need js to handle

JS Code:

<script>        $(function(){            $(". Input-fileup"). On ("Change", "input[type= ' file ']",function(){                varfilepath=$ ( This). Val (); if(Filepath.indexof ("jpg")!=-1 | | filepath.indexof ("PNG")!=-1){                    $(". FileerrorTip1"). HTML (""). Hide (); varArr=filepath.split (' \ \ '); varFilename=arr[arr.length-1]; $(". ShowFileName1"). HTML (fileName); }Else{                    $(". ShowFileName1"). HTML (""); $(". FileerrorTip1"). HTML ("You did not upload the file, or you uploaded a file of the wrong type!"). "). Show (); return false                }            })        })    </script>

Also need to add two div to HTML

<aclass= "Input-file input-fileup"href= "javascript:;">+ Select File<inputsize= "+"type= "File"name= "File"ID= "File"></a>
<Divclass= "FileerrorTip1"></Div><Divclass= "ShowFileName1"></Div>

Effect:

input[tyle= "file"] style modification and upload file name display

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.