简体   繁体   English

验证CheckBox字段-ASP.NET和jQuery

[英]Validating a CheckBox field - ASP.NET and jQuery

I am trying to make a simple validation on some agreements by checking an asp:checkbox and clicking an asp:button. 我试图通过检查一个asp:复选框并单击一个asp:按钮来对某些协议进行简单的验证。

On my Button I am calling this: 在我的按钮上,我这样称呼:

OnClientClick="ValidateConditions();" OnClientClick =“ ValidateConditions();”

And the function runs by this: 该函数由此运行:

function ValidateConditions() {
 if ($('#ChkAccept').is(':checked')) {
             alert("Checked");
                  args.IsValid = true;
               }
                else {
                   alert('Nope it is not checked');
                args.IsValid = false;
           }
         }

It works so far, but how do I stop running my button method when args.IsValid = false; 到目前为止,它仍然有效,但是当args.IsValid = false时,如何停止运行按钮方法? ? Right now it shows the message and runs the method in both cases. 现在,它显示消息并在两种情况下都运行该方法。

It must be a simple thing that I need.. Best regards. 这一定是我需要的一件简单的事情。最好的问候。

do the below to prevent the event of the button :-- but do also OnClientClick="return ValidateConditions(this); in your markup code 请执行以下操作以防止按钮发生事件:-但在标记代码中也请执行OnClientClick =“ return ValidateConditions(this);

 function ValidateConditions(element,e) {
    if ($(element).prop('checked')) {
        alert("Checked");
        args.IsValid = true;
    }
    else {
        alert('Nope it is not checked');
        args.IsValid = false;
e.preventDefault();
e.stopPropagation();
return false ;
    }

}

Removed all other javascripts: 删除了所有其他JavaScript:

Just use: 只需使用:

   <asp:CheckBox ID="ChkAccept" runat="server" ClientIDMode="Static" Text="I accept the conditions" /> 
    <br />
   <asp:Button ID="BtnConfirm" runat="server" CssClass="uniBtn" Text="Send"
   OnClientClick="if(! $( '#ChkAccept' ).prop( 'checked' )){alert('Not Ticked');return false;}" />  

Try: 尝试:

OnClientClick="return ValidateConditions(this);"

and

function ValidateConditions(checkbox) {
    return $(checkbox).is(':checked');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM