2014-12-13 2 views
0

여러 입력 유형 파일이 있습니다. 기본적으로 숨겨져 있으며 첫 번째 항목 만 표시됩니다. 사용자가 첫 번째 입력 파일이있는 파일을 선택하면 아래 기능은 id로 다음 입력 파일을 보여줍니다. 문제는 첫 번째 2 변경 이벤트에만 적용되고 오류없이 중지하는 것입니다. 어떻게 다음 입력 파일마다 작동하게 할 수 있습니까?Jquery 입력 유형 파일이 트리거로 작동하지 않습니다.

여기
//first we hide all inputs 
$('.imagediv').hide(); 
$('.imagediv:first').show(); 
var nextupload=$('.imagediv:first').attr('data'); 
nextupload=(parseInt(nextupload) + 1); 

//this is the event 
$(function() { 
$("input:file").change(function(){ 
    var nextf = $(this).attr('data'); 
    $('#input'+(nextupload)+'').show(); 
    alert('#input'+(nextupload)+''); 
});  
}); 

는 HTML의 모습 방법은 다음과 같습니다 : 여기에 기능입니다

<div class="imagediv" id="input2" data="2"> 
<input type="file" name="gallery_upload_2" data="gallery_upload_3" id="gallery_upload_2" /> 
</div> 

등 다음 입력으로는 .. 는 어떻게 모든 다음 입력 변화를 작동하게하는? 감사합니다

는 여기 jsfiddle했다 : http://jsfiddle.net/Lbu3ksjh/1/

+0

는 $ ("입력 : 파일") 대신이 사용 $ ("#의 gallery_upload_2") –

+0

어떤 것 이 변화? 나는 이벤트를 놓치고 있지 않다. $ ("input : file")을 사용하여 잘 작동하지만, 단지 2 번만 실행한다. – Europeuser

답변

2

당신은 변화 함수에서 +1 일부 누락되었다가 :

Demo

+0

경험에 감사한다. $ {function() { $ ('.imagediv ') .hide(); $ ('.imageiv : first '). show(); $ ("입력 : 파일"). (function() { var nextf = $ (this) .attr (' 데이터 '); \t $ ('# 입력 '+ (nextf) +' ') 다음(). 그러나 stil 나는 투표 할 것이고 당신의 대답 (감사)을 받아 들일 것이다! – Europeuser

+0

고마워, 천만에. – empiric

2

:

$('input:file').change(function(){ 
    var nextupload = $(this).parent('.imagediv').attr('data'); 
    nextupload = (parseInt(nextupload) + 1); 
    $('#input'+(nextupload)+'').show(); 
    alert('#input'+(nextupload)+''); 
}); 

나는 당신의 바이올린 업데이트 여기에 또 다른 해결책이 있습니다. 다양한 파일 입력에서 작동하며 많은 코드 나 마크 업이 필요하지 않습니다.

DEMO

JS

$(function() { 
    var $fileInputs = $(':file[name*="gallery_"]'), // could be whatever 
     next = 0; 

    $fileInputs.not(':first').hide(); 

    $fileInputs.change(function(){ 
     $fileInputs.eq(++next).show(); 
    }); 
}); 

HTML

<input type="file" name="gallery_upload_1" /> 
<input type="file" name="gallery_upload_2" /> 
<input type="file" name="gallery_upload_3" /> 
<input type="text" name="random_text_input" /> 
<!-- notice it only reveals file inputs --> 
<input type="file" name="gallery_upload_4" /> 
관련 문제