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