【问题标题】:Validate Second field if First Field is filled如果第一个字段已填写,则验证第二个字段
【发布时间】:2011-06-09 14:37:38
【问题描述】:

我有 3 个文件浏览器和每个文件输入类型的标签文本框。

<label>Photo 1</label>
<input type="file" size="10" name="service_field[15]" id="15" class="{accept:'jpg|jpeg|png|gif|bmp|tif',messages:{accept:'Only jpg,jpeg,gif,png,bmp,tif files are allowed.'}} file">
<label>Photo 1 Title</label>
<input type="text" name="service_field[576]" id="576" class="txt" value="">

<label>Photo 2</label>
<input type="file" size="10" name="service_field[578]" id="578" class="{accept:'jpg|jpeg|png|gif|bmp|tif',messages:{accept:'Only jpg,jpeg,gif,png,bmp,tif files are allowed.'}} file">  
<label>Photo 2 Title</label>
<input type="text" name="service_field[577]" id="577" class="txt" value="">

<label>Photo 3</label>
<input type="file" size="10" name="service_field[579]" id="579"  class="{accept:'jpg|jpeg|png|gif|bmp|tif',messages:{accept:'Only jpg,jpeg,gif,png,bmp,tif files are allowed.'}} file" >    
<label>Photo 3 Title</label>
<input type="text" name="service_field[580]" id="580" class="txt" value="">

我正在使用带有 jquery.metadata.js 的 jquery 验证器插件。

如果浏览了文件,我需要以这样的方式验证这些输入,然后用户必须输入该文件的标签。

【问题讨论】:

  • 仅供参考,您的标签应该具有for 属性,或者应该包含它们所引用的字段。这样,点击标签会将焦点放在相应的字段上。
  • 仅供参考(再次)jquery.metadata.js 实际上已过时。从 jQuery 1.4 开始,$().data() 可以访问 JSON 编码的数据属性。此外,将 json 文字放在 class 属性中是一个坏主意。

标签: jquery jquery-plugins jquery-validate


【解决方案1】:

你需要用一个像这样的 div 来包装它们

<div>
   <label>Photo 1</label>
   <input type="file" size="10" name="service_field[15]" id="15" class="{accept:'jpg|jpeg|png|gif|bmp|tif',messages:{accept:'Only jpg,jpeg,gif,png,bmp,tif files are allowed.'}} file">
   <label>Photo 1 Title</label>
   <input type="text" name="service_field[576]" id="576" class="txt" value="">
</div>

那你就可以了

$('.txt').siblings('[type=file]').bind('change blur',function(){//if it changes or loses focus
     if($(this).val().length){
          $(this).siblings('.txt').show();
     }else{
          $(this).siblings('.txt').show();
     }
})
.blur();//this is so it can be trigger every time you load the page,, comment out if you wish

我应该提到...我没有使用该验证,所以给你一个替代它做同样的事情:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2018-07-13
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多