开发者

Why Custom Validator doesnt work in the document.ready of jQuery

开发者 https://www.devze.com 2023-03-13 23:12 出处:网络
I call a function trought my Custom validator on .NET : <asp:CheckBox ID=\"chbNota\" runat=\"server\" />

I call a function trought my Custom validator on .NET :

<asp:CheckBox ID="chbNota" runat="server" />
<asp:CustomValidator ClientValidationFunction="RequiredPrivacy" Runat="server" ID="cvPrivacy" onservervalidate="CustomValidatorchkPrivacy_ServerValidate" >&nbsp;*</asp:CustomValidator>

Now, If I declare the function into $(document).ready(function() as :

$(document).ready(function() {
    function R开发者_如何学运维equiredPrivacy(oSrc, args) {
        if (!$('#<%=chbNota.ClientID%>').prop("checked")) args.IsValid = false;
    }
});

the whole Validator on client side go to true.

Why this behaviour? If I move that function out of $(document).ready(function() all work perfectly...


When you define the function inside your "ready" handler, it's hidden from the global scope. It's like a local variable just for the handler function, in other words.

There's no need to define functions intended to be globally visible inside a "ready" handler. If you really want to, however, you could do this:

$(document).ready(function() {
    window['RequiredPrivacy'] = function(oSrc, args) {
        if (!$('#<%=chbNota.ClientID%>').prop("checked")) args.IsValid = false;
    }
});


The function must be declared in the global namespace. When you place the function within $(document).ready, it scopes the function and prevents the built in CustomValidator javascript hooks from finding that function.

0

精彩评论

暂无评论...
验证码 换一张
取 消