2017-07-27 75 views
3

我有一个输入字段,用于存储检查条件的vales.I将它改为hidden.if条件为false我需要显示一条消息。如何使用jquery在隐藏字段中显示消息

如果该字段被隐藏,则不显示消息。

HTML

<input type="hidden" id="year1_req_fund_hidden" name="year1_req_fund_hidden"> 

jQuery的

#wizard5').validate(
{ 
    rules: { 

     year1_req_fund_hidden:{ 
        //required:true, 
        number:true, 
        validate_check: [$('#year1_grand_amt').val(),'1'], 
        }, 
      }, 
}); 


$.validator.addMethod("validate_check", 
    function(value, element, params) { 
    if(params[0]!=''){ 
    if(value == params[0]) { 
    return true; 
    } 
    } 
    }, 
$.format("The total amount requested does not match with grant amount {0} requested in Q1.11 in year {1} .") 

); 

请人helpm我

+0

这会给你一些想法https://stackoverflow.com/questions/6966221/how-to-check-if-an-input-element-is-hidden – Saurabh

回答

1

这样可以解决你的问题... ...!

if (!$('input').attr('id') == null) { 
 
    //check your condition according to you 
 
    //your action here 
 
} else { 
 
    //show your message in .hidden div 
 
    $('.hidden').fadeIn('slow').html('Message'); 
 
}
.hidden {display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="hidden" id="year1_req_fund_hidden" name="year1_req_fund_hidden"> 
 
<!-- Place below div where you want to show your message --> 
 
<div class="hidden"></div>

1
$('#wizard5').validate({ 
    ignore:'', // or ignore: [] 
    rules: { 

     year1_req_fund_hidden:{ 
       //required:true, 
       number:true, 
       validate_check: [$('#year1_grand_amt').val(),'1'], 
      }, 
    }, 
}); 

你的代码应该按照上述

它会为你工作。默认情况下,jQuery验证忽略隐藏的字段。 http://prntscr.com/g0urkk

+0

它不是沃金的兄弟 – robins

+0

https://开头jqueryvalidation.org/validate/ 请参阅此链接。可能是你的validate_check函数不工作 –