2013-03-01 111 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/