从 ascx 调用 jquery 函数不起作用

发布于 2024-09-04 20:06:52 字数 3811 浏览 3 评论 0原文

我遇到以下情况的问题。

我有一个 ascx,其中包含一个搜索条件的提交按钮,我试图在整个网站中使用的 js 文件中调用验证函数(这是我第一次在 ascx 中使用它)。

现在我刚刚尝试了这个:

<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/jsAdmin_Generic_SystemValidation.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".submitBtn").click(function (e) {
            alert("test");
            alert($.Validate());
            alert("test 2");
        });
    });
</script>

该文件被正确引用,因为我已经在 Firebug 中看到了由它完成的帖子。

这是函数:

jQuery.extend({
Validate: function () {
    var requiredElements = $('.required').length; // Get the number of elements with class required

    $('.required').each(function () {
        // If value of textbox is empty and have not
        // yet been validated then validate all required
        // elements. i.e.
        if (($(this).val() == "") || ($(this).hasClass("validationError")) || ($(this).hasClass("validationAlert")) || ($(this).hasClass("validationOk") == false)) {
            validate($(this));
        }
    });

    if ($('.validationOk').length == requiredElements) {
        return true;
    } else {
        return false;
    }
},
// Another extended function, this function
// is used for pages with the edit-in-place
// feature implemented.
validateElement: function (obj) {
    var elementId = obj.attr("id"); // id of the button clicked.
    var flag = 0;

    if (elementId.toLowerCase() == "paymentmethodid") {
        // Case elementId = paymentMethodId then check all the 
        // elements with css class starting with openStorage
        var requiredElements = $(document).find("input[class*='openStorage']").length; // Get the number of elements with css class starting with openStorage

        // Loop through all the elements with css class containing
        // openStorage abd validate each element.
        $(document).find("input[class*='openStorage']").each(function () {
            if (($(this).val() == "") || ($(this).hasClass("validationError")) || ($(this).hasClass("validationAlert"))) {
                validate($(this));
            }

            if ($(this).hasClass("validationOk")) {
                flag++;
            } else if (($(this).hasClass("validationError")) || ($(this).hasClass("validationAlert"))) {
                flag--;
            }
        });

        // If all elements are valid return true else return false
        if (flag == requiredElements) {
            return true;
        } else {
            return false;
        }
    } else if (elementId.toLowerCase() == "registeredfortax") {
        if (($('.TaxRegistrationNumber').val() == "") || ($('.TaxRegistrationNumber').hasClass("validationError")) || ($('.TaxRegistrationNumber').hasClass("validationAlert"))) {
            validate($('.TaxRegistrationNumber'));
        }

        if ($('.TaxRegistrationNumber').hasClass("validationOk")) {
            return true;
        } else {
            return false;
        }
    } else {
        var elementClass = "." + elementId;

        if (($(elementClass).val() == "") || ($(elementClass).hasClass("validationError")) || ($(elementClass).hasClass("validationAlert")) || ($(elementClass).hasClass("validationOk") == false)) {
            validate($(elementClass));
        }

        if ($(elementClass).hasClass("validationOk") && ($(elementClass).hasClass("required"))) {
            return true;
        } else if ($(elementClass).hasClass("required") == false) {
            return true;
        }else {
            return false;
        }
    }
}
});

现在,我首先在 firebug 中得到“Validate() 不是函数”。自从我进行了警报测试以来,我收到了第一个警报,然后没有任何错误。

任何人都可以透露一些信息吗?

谢谢

I'm having a problem with the following situation.

I have an ascx which contains a submit button for a search criteria and I am trying to call a validation function in a js file I've used throughout the site (this is the first time I'm using it in an ascx).

Now I've just tried this:

<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/jsAdmin_Generic_SystemValidation.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".submitBtn").click(function (e) {
            alert("test");
            alert($.Validate());
            alert("test 2");
        });
    });
</script>

The file is being referenced correctly as I am already seeing posts in Firebug that are done by it.

This is the function:

jQuery.extend({
Validate: function () {
    var requiredElements = $('.required').length; // Get the number of elements with class required

    $('.required').each(function () {
        // If value of textbox is empty and have not
        // yet been validated then validate all required
        // elements. i.e.
        if (($(this).val() == "") || ($(this).hasClass("validationError")) || ($(this).hasClass("validationAlert")) || ($(this).hasClass("validationOk") == false)) {
            validate($(this));
        }
    });

    if ($('.validationOk').length == requiredElements) {
        return true;
    } else {
        return false;
    }
},
// Another extended function, this function
// is used for pages with the edit-in-place
// feature implemented.
validateElement: function (obj) {
    var elementId = obj.attr("id"); // id of the button clicked.
    var flag = 0;

    if (elementId.toLowerCase() == "paymentmethodid") {
        // Case elementId = paymentMethodId then check all the 
        // elements with css class starting with openStorage
        var requiredElements = $(document).find("input[class*='openStorage']").length; // Get the number of elements with css class starting with openStorage

        // Loop through all the elements with css class containing
        // openStorage abd validate each element.
        $(document).find("input[class*='openStorage']").each(function () {
            if (($(this).val() == "") || ($(this).hasClass("validationError")) || ($(this).hasClass("validationAlert"))) {
                validate($(this));
            }

            if ($(this).hasClass("validationOk")) {
                flag++;
            } else if (($(this).hasClass("validationError")) || ($(this).hasClass("validationAlert"))) {
                flag--;
            }
        });

        // If all elements are valid return true else return false
        if (flag == requiredElements) {
            return true;
        } else {
            return false;
        }
    } else if (elementId.toLowerCase() == "registeredfortax") {
        if (($('.TaxRegistrationNumber').val() == "") || ($('.TaxRegistrationNumber').hasClass("validationError")) || ($('.TaxRegistrationNumber').hasClass("validationAlert"))) {
            validate($('.TaxRegistrationNumber'));
        }

        if ($('.TaxRegistrationNumber').hasClass("validationOk")) {
            return true;
        } else {
            return false;
        }
    } else {
        var elementClass = "." + elementId;

        if (($(elementClass).val() == "") || ($(elementClass).hasClass("validationError")) || ($(elementClass).hasClass("validationAlert")) || ($(elementClass).hasClass("validationOk") == false)) {
            validate($(elementClass));
        }

        if ($(elementClass).hasClass("validationOk") && ($(elementClass).hasClass("required"))) {
            return true;
        } else if ($(elementClass).hasClass("required") == false) {
            return true;
        }else {
            return false;
        }
    }
}
});

Now at first I was getting "Validate() is not a function" in firebug. Since I did that alert testing, I am getting the first alert, then nothing with no errors.

Can anyone shed some light?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-09-11 20:06:52

您是否正确使用了扩展方法? ...
http://api.jquery.com/jQuery.extend/

Are you using the extend method properly? ...
http://api.jquery.com/jQuery.extend/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文