2014-07-22 14 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

检查更新的代码以验证该文件是否为图像。 –