input type=file显示及清空值


//  文件上传:只显示文本框,当鼠标经过,显示input type=file
$( function() {
    $(".up_file").each( function() {
         var container =  this;
         var txtSrc = $("input[type='text']",  this);
         var fileObj = $("input[type='file']",  this);

        txtSrc.mouseover( function() {
            txtSrc.hide();
            fileObj.show();
        })

        fileObj.mouseout( function() {
            fileObj.hide();
            txtSrc.show();

             if (fileObj.val().length > 0) {
                txtSrc.val(fileObj.val());
            }
        });
    });
})

//  清空文件上传控件
//
      不能直接用js修改input type=file的value,但可以通过form的reset()清空它的值
//
      解决:将input type=file放进一个临时form,清空value,再将它移到原来位置
function emptyFileUpload(selector) {
     var fi;
     var sourceParent;

     if (selector) {
        fi = $(selector);
        sourceParent = fi.parent();
    }
     else {
         return;
    }

    $("<form id='tempForm'></form>").appendTo(document.body);

     var tempForm = $("#tempForm");

    tempForm.append(fi);
    tempForm.get(0).reset();

    sourceParent.append(fi);
    tempForm.remove();
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM