请选择汽车类型:

ASP.NET jQuery 食谱15 (通过控件CustomValidat

系统 1474 0

这节我们要讲下如何通过控件CustomValidator来校验CheckBoxList,客户端校验代码通过jQuery实现。

首先看下界面代码:

      
        <
      
      
        form 
      
      
        id
      
      
        ="form1"
      
      
         runat
      
      
        ="server"
      
      
        >
      
      
< div align ="center" >
< fieldset style ="width: 350px; height: 200px;" >
< table border ="0" cellpadding ="3" cellspacing ="3" >
< tr >
< td >
请选择汽车类型:
</ td >
</ tr >
< tr >
< td >
< asp:CheckBoxList ID ="chkCar" runat ="server" >
< asp:ListItem Value ="1" Text ="奔驰汽车" ></ asp:ListItem >
< asp:ListItem Value ="2" Text ="宝马汽车" ></ asp:ListItem >
< asp:ListItem Value ="3" Text ="奥迪汽车" ></ asp:ListItem >
< asp:ListItem Value ="4" Text ="现代汽车" ></ asp:ListItem >
< asp:ListItem Value ="5" Text ="丰田汽车" ></ asp:ListItem >
</ asp:CheckBoxList >
</ td >
</ tr >
< tr >
< td >
< asp:Button ID ="btnSubmit" runat ="server" Text ="提交" />
</ td >
</ tr >
</ table >
</ fieldset >
< asp:CustomValidator ID ="ctvCar" runat ="server" Display ="Dynamic" ErrorMessage ="至少选择三种车!"
ForeColor
="Red" ClientValidationFunction ="ctvCar_Validation" ></ asp:CustomValidator >
</ div >
</ form >

控件CustomValidator相关参数说明:

ErrorMessage:设置校验提示信息

Display:设置显示模式,Dynamic表示提示信息不显示的时候,不占用当前位置

ClientValidationFunction:设置定义客户端自定义校验函数

客户端脚本代码:

      
        <
      
      
        head 
      
      
        id
      
      
        ="Head1"
      
      
         runat
      
      
        ="server"
      
      
        >
      
      
< title > Recipe15 </ title >
< script type ="text/javascript" src ="Scripts/jquery-1.4.1-vsdoc.js" ></ script >
< script type ="text/javascript" >
function ctvCar_Validation(sender, args) {
// CustomValidator控件通过自定义函数的参数args属性IsValid来判断是否验证通过
args.IsValid = ($( " #chkCar input:checked " ).length >= 3 );
}
</ script >
</ head >

最终显示效果:

ASP.NET jQuery 食谱15 (通过控件CustomValidator验证CheckBoxList)_第1张图片





ASP.NET jQuery 食谱15 (通过控件CustomValidator验证CheckBoxList)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论