2010-02-23 9 views
0

아래 코드는 업로드를 위해 선택된 파일 목록을 가져옵니다.imput 요소를 반복하여 배열에 값을 할당

그것은 기본적으로 폼 요소 위의 사업부 내부에 입력 요소를 추가합니다 :

<div id = "files_list"> </div> 

어떻게 내가 저장 배열의 모든 속성 이름 - 제출 버튼을 클릭에 - fileNamesArray.?

// 시도의 시작

// 내 접근 : 어떤 파일이 업로드 선택하지 않은 경우 // 사용자에게 경고이 작동하는지

시도 나는 확인하기 위해 아직이야

// 입력 요소를 반복하고 div id = "files_list"에 포함 된 모든 파일 이름을 가져 와서 모든 값을 배열 $ filesArray으로 푸시해야합니다.

// 거친 시도

$("Submit").click(function() { 

    $filesArray 

    $(div#files_list).getElementById('input').each(function($filesArray) { 
     filesArray.push($this.attr("value")) 
    }); 

    while($filesArray.size != 0) { 
     document.writeln("<p>" + $filesArray.pop() + "</p>"); 
    } 
} 
시도

// 끝 : 나는 이름을 인쇄 바로 아래

코드 확인 : HTML 양식에 대한

$(document).ready(function(){ 
var fileMax = 6; 
$('#asdf').after('<div id="files_list" style="border:1px solid #666;padding:5px;background:#fff;" class="normal-gray">Files (maximum '+fileMax+'):</div>'); 
$("input.upload").change(function(){ 
doIt(this, fileMax); 
}); 
}); 

function doIt(obj, fm) { 
if($('input.upload').size() > fm) {alert('Max files is '+fm); obj.value='';return true;} 
$(obj).hide(); 
$(obj).parent().prepend('<input type="file" class="upload" name="fileX[]" />').find("input").change(function() {doIt(this, fm)}); 
var v = obj.value; 
if(v != '') { 
$("div#files_list").append('<div>'+v+'<input type="button" class="remove" value="Delete" style="margin:5px;" class="text-field"/></div>') 
.find("input").click(function(){ 
$(this).parent().remove(); 
$(obj).remove(); 


return true; 
}); 
} 
}; 

코드 :

<td><form action="test.php" method="post" enctype="multipart/form-data" name="asdf" id="asdf"> 
     <div id="mUpload"> 
    <table border="0" cellspacing="0" cellpadding="8"> 
     <tr> 
     <td><input type="file" id="element_input" class="upload" name="fileX[]" /></td> 
     </tr> 
     <tr> 
     <td><label> 
      <textarea name="textarea" cols="65" rows="4" class="text-field" id="textarea">Add a description</textarea> 
     </label></td> 
     </tr> 
     <tr> 
     <td><input name="Submit" type="button" class="text-field" id="send" value="Submit" /></td> 
     </tr> 
     </table><br /> 
     </div> 
</form> 

    <p class="normal"></td> 

답변

1
var my_array = new Array(); 

$('#asdf').bind('submit', function() { 
    $.each(this.elements, function() { 
     if (this.type == 'file') { 
      $('#file_list').append($(this).clone()); 
      my_array.push(this.value); 
     } 
    }); 

    for (var i=0; i < my_array.length; i++) 
     alert(my_array[i]); 
}); 

여기 있습니다.

수정 OP의 의견으로 인해 업데이트되었습니다.

+0

귀하의 솔루션을 알아 내려고 노력해 주셔서 감사합니다. 몇 가지 빠른 질문이 있습니다. 평가 this.type == "file"이 (가) 속성 유형 = "파일"의 요소에 대해 true로 평가된다는 것을 알고 있습니다. 파일 이름에 대한 참조를 보유하고있는 배열의 이름은 무엇입니까? 나는 당신이 input 요소의 값을 if 블록 뒤에 fileNames 배열에 할당하는 것을 이해할 수 없다 ?? 솔루션의 마지막 "fileNames"배열에 요소 "파일 이름"을 어떻게 인쇄합니까? 건배 그런 다음 배열의 값을 어떻게 인쇄 하시겠습니까? – Terman

+0

귀하의 필요에 맞게 게시물을 업데이트했습니다. 건배. – aefxx

관련 문제