2013-11-25 3 views
1

은 내가 이런 구조 코드가있는 경우 그것을 할 수있는 방법을 특정 입력 파일jQuery를 - ... 특정 필드는

에서 값을 재설정 할 가치

<div id="row_btnbar1" class="buttonbar"> 
    <div> 
     <div class="gallery"> 
      <output id="show_img1"></output> 
     </div> 
     <div class="button"> 
      <input type="file" id="files1" name="files[]"/> 
      <button id="reset1" type="reset" class="reset"> 
       <span>Reset</span> 
      </button> 
      <span class="process"></span> 
     </div> 
    </div> 
</div> 

<div id="row_btnbar2" class="buttonbar"> 
    // #files2, #reset2 
</div> 
<div id="row_btnbar3" class="buttonbar"> 
    // #files3, #reset3 
</div> 
<div id="row_btnbar4" class="buttonbar"> 
    // #files4, #reset4 
</div> 

답변

0

HTML 파일을 다시 설정하는 방법 입력을 일반적인 방법 (예 : 값을 null 또는 빈 문자열로 설정)으로 지울 수 없지만 브라우저 보안 제한으로 인해 대부분의 브라우저는이를 허용하지 않습니다. 많은 브라우저에서 null 값 속성을 설정해도 효과가 없거나 오류가 발생할 수 있습니다. 대신 이전 요소를 복제하고 두 요소를 서로 바꿔주는 새 요소를 만듭니다.

읽기 : How to clear HTML file inputs


Trick:- You have to create new input type file and replace it with old one.


JS

function clearFileInput() { 
    var oldInput = document.getElementById("files1"); 
    var newInput = document.createElement("input"); 
    newInput.type = "file"; 
    newInput.id = oldInput.id; 
    newInput.name = oldInput.name; 
    newInput.className = oldInput.className; 
    newInput.style.cssText = oldInput.style.cssText; 
    // copy any other relevant attributes 
    oldInput.parentNode.replaceChild(newInput, oldInput); 
} 

fiddle Demo


영업 이익의 코멘트 후 업데이트

Updated fiddle Demo

$(document).ready(function() { 
    function clearFileInput(el) { 
     var oldInput = document.getElementById(el); 
     var newInput = document.createElement("input"); 
     newInput.type = "file"; 
     newInput.id = oldInput.id; 
     newInput.name = oldInput.name; 
     newInput.className = oldInput.className; 
     newInput.style.cssText = oldInput.style.cssText; 
     // copy any other relevant attributes 
     oldInput.parentNode.replaceChild(newInput, oldInput); 
    } 
    $('.reset').click(function() { 
     clearFileInput($(this).prev('input[type="file"]').attr('id')); 
    }); 
}); 
+0

이, 내가 채워지고 단지 현재의 입력 파일을 재설정 할 전체 코드 미리보기 http://jsfiddle.net/7EVS4/1/입니다. – gierg

+0

@gierg check updated 답변. –

+0

hwaa .... 고마워 ... 일이야! :) thx ...;) – gierg