2014-07-22 15 views
0

區分每個file_upload控制在我的應用程序正在使用循環即低於生成多個file_upload控制:如何通過ID RoR中

<div class='row' id='selfintroduction'> 
    <% @sections.each do |s| %> 
     <span class='label' style="font-size:small;font-weight:bold"> 
      <%= s.SectionName %> 
     </span> 
     <br/> 
     <span class='formw' style="font-size:small"> 
      <input type='file' id='Self' name='Self'/> // File upload control 
     </span> 
    <% end %> 
</div> 

@sections循環我有SectionName,我與每一起產生file_upload控制SectionName。我想在在onchange方法file_upload控制使用jQuery適用file_type驗證,但問題是,多個file_upload控制具有相同的ID IE

<input type='file' id='Self' name='Self'/> 

然後我就用file_upload控制沿着連接SectionNameid

<input type='file' id='Self[<%= s.SectionName %>]' 
下面

onchange方法,其中,我使用idfile_upload控制值獲得:

<script type="text/javascript"> 
    $(document).ready(function() { 
     var i = 0; 
     $("#Self["+ i +"]").on('change',function() { 
      alert("Self["+ i +"]"); 
     }); 
    }); 
</script> 

但在上述onchange方法我無法得到file_upload控制idSectionName區分每個file_upload控制。我怎樣才能做到這一點?

請告訴我,等待回覆。 感謝

+0

爲什麼你不使用類? –

+0

我如何在這裏使用課堂,請解釋我。 – user3860097

回答

0

HTML:

<input type='file' id='Self' class="inputfileupload" name='Self'/> 

JQUERY:

$(document).ready(function(){ 
     $(".inputfileupload").change(function() { 
     alert($(this).attr('id')); 

     var url = this.value; 
     var ext = url.substring(url.lastIndexOf('.') + 1).toLowerCase(); 
     if (input.files && input.files[0]&& (ext == "gif" || ext == "png" || ext == "jpeg" || ext == "jpg")) { 

      alert("Valid image"); 
     }else{ 
      alert("Image format not valid"); 
     } 
     }); 

    }); 

檢查this堆,以驗證多文件上傳(你可以使用jQuery驗證)。

+0

使用你的代碼我得到'file_upload'控件的id,但我也想通過這個id獲得'file_upload'的值。請建議我 – user3860097

+0

檢查更新的代碼以驗證該文件是否爲圖像。 –