2013-03-01 108 views
1

我有一个领域与显示:无property.Its像,当用户点击“是”单选按钮然后文件上传字段将出现和验证发生的文件上传也。在这种情况下,我用ignore [ ]来验证工作,但会发生什么,即使我点击否,然后验证文件上传正在发生。如何使用style = display:none验证字段?

这里是我的代码

<li > 
    <p> 
    <label for="rad">radio label: 
    </label><br> 
    <input type="radio" name="rad" value="yes" style="width:20px">&nbsp; Yes&nbsp;&nbsp;</input> 
    <input type="radio" name="rad" value="no" style="width:20px">&nbsp; No</input><br/> 
    <label for="rad" class="error" style="display:none">This field is required</label> 
    </p> 
    <p> 
     <input type="file" name="fupl" style="display:none;margin-top:10px" id="fup"/> 
     <label for="fupl" class="error" style="display:none;color:red">This field is required</label> 
    </p> 
</li> 
<br> 
<li> 
    <label>checkbox label 
    </label><br><br> 
    <input type="checkbox" name="cb" value="tick" style="width:20px">&nbsp;&nbsp;<small>checkbox field<small></input> 
    <br> 
    <label for="fee" class="error" style="display:none">This field is required</label> 
</li> 
<br> 
<li> 
<input type="submit" class="button" value="SUBMIT" style="float:right"/> 
</li> 
<script> 
     $(document).ready(function() 
     { 
     $("input[type='radio']").change(function(){ 
     if($(this).val()=="yes") 
     { 
     $("#fup").show(); 
     } 
     else 
     { 
     $("#fup").hide(); 
     } 
     }); 
     }); 
    </script> 

这是我的jQuery

$('#form').validate({ 
    ignore :[], 
    rules: { 
     fupl: { 
      required: true, 
      accept:'docx|doc' 
      }, 
     cb: 
      { 
      required:true 
      } 
     } 
     }); 
+0

正如我表明[你在我这里的答案](http://stackoverflow.com/a/15148001/594235),您的代码似乎已经在做你所问的问题了。但是你的标题说明了这一点:_“问:如何使用style = display:none验证字段?”_如果你不想验证隐藏字段,则删除'ignore:[]'选项。然后,我添加了代码以隐藏该字段变为隐藏时的错误:http://jsfiddle.net/y5ghU/4/ – Sparky 2013-03-01 17:23:34

+0

Yeeeep ....对我来说看起来也是一个骗局。 – Ryley 2013-03-01 21:32:21

回答

0

使用fup代替fup1在验证脚本。

+0

fup是我的id.fupl是该字段的名称。为什么在我的脚本中使用它 – lakshganga 2013-03-01 13:14:55

0

您可以在那里随时添加规则的文件上传你的变化的功能,而不是让它们:

var fuplRules = { 
    required: true, 
    accept: 'docx|doc' 
}; 


$("input[type='radio']").change(function() { 
    if ($(this).val() == "yes") { 
     $("#fup").show().rules('add', fuplRules); 
    } else { 
     $("#fup").hide().rules('remove'); 
    } 
}); 

或者,你可以保持你的设置,现在是这样的,只是改变required: truerequired: 'input[name="rad"][value="yes"]:checked'。这使用所需规则的dependency-expression版本。

工作实例1:http://jsfiddle.net/ryleyb/WFKfq/

工作实例2:http://jsfiddle.net/ryleyb/WFKfq/1/