Why Custom Validator doesnt work in the document.ready of jQuery
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" > *</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.
精彩评论