JQuery抛出一个错误" Uncaught TypeError:不是一个函数"

时间:2016-12-02 14:43:57

标签: jquery jquery-ui jquery-plugins

我是JQuery的新手,下面是我的jquery代码,有两个函数1和2.

返回opts.isValidDate(); <功能:1 引发错误&#34;未捕获的TypeError:opts.isValidDate不是函数& #34;

有什么想法吗?

谢谢!

功能:1

(function ($) {
var old = $.fn.datepicker;    
jQuery.fn.extend({
    datepicker: function (opts) {

        return this.each(function () {

            var $this = $(this);

            if ($this.data("datepickerinit")) {
                //return;
            }

            $this.data("datepickerinit", true);

            this.isValidDate = function () {
                return opts.isValidDate;
            }

            var obj = $this.ParseClass();
            var defaults = {
                nextText: "&gt;",
                prevText: "&lt;",
                showAnim: 'blind',
                onSelect: function (dateText, inst) {
                    $this.trigger("dpOnSelect", { dateText: dateText, inst: inst });
                },
                showAddressConnectDates: false,
                showDisconnectDates: false,
                onChangeUpdateOptions: null
            };

            $.extend(opts, defaults, obj);

            if (opts.readonly) {
                $this.attr("readonly", true);
            }

            return old.call($this, opts);
        });

        //return this;
    }
});

功能:2

        var old_generateHTML = $.datepicker._generateHTML;
    $.datepicker._generateHTML = function (inst) {
        var ret = '';
        if (inst.settings.prepend) {
            ret += unescape(inst.settings.prepend);
        }
        ret += old_generateHTML.call(this, inst);
        if (inst.settings.append) {
            ret += unescape(inst.settings.append);
        }
        return ret;
    }

    $.datepicker.dpDiv.bind("click", function (e) {
        if ($(e.target).is("a.closelink")) {
            $.datepicker._hideDatepicker();
        }
    });

    $.datepicker.init = function (sel) {
        $(sel || "input.date").each(function () {
            var opts =  {};
            var $this = $(this);
            var thisInstance = this;

            $this.config = {
                openDateClass: "",
                invalidInputClass: "",
                defaultDate: new Date(),
                allowedDays: [1, 2, 3, 4, 5],
                availableSaturday: false,
                specialDates: {},
                selectDefaultDate: true,
                priorityConnectFee: "$0",
                StanderdConnectFee: "$0",
                append: "",
            };

            $this.update = function (data) {
                var obj = $this.ParseClass();
                var defaults = {
                    nextText: "&gt;",
                    prevText: "&lt;",
                    showAnim: 'blind',
                    onSelect: function (dateText, inst) {
                        $this.trigger("dpOnSelect", { dateText: dateText, inst: inst });
                    },
                    showAddressConnectDates: false,
                    showDisconnectDates: false,
                    onChangeUpdateOptions: null
                };
                if (data != null) {
                    $this.resetCalendar();
                    $this.config.openDateClass = "ui-datepicker-open";
                    $this.config.meterType = data.meterType;
                    $this.config.specialDates = data.specDates || $this.config.specialDates || {};
                    $this.config.availableSaturday = data.availableSaturday || $this.config.availableSaturday || false;
                    $this.config.priorityConnectFee = data.priorityConnectFee || "$0";
                    $this.config.StanderdConnectFee = data.StanderdConnectFee || "$0";
                    $this.config.append = data.append;

                    if (data.defaultDate)
                        $this.config.defaultDate = new Date(data.defaultDate);
                    if (data.availableSaturday)
                        $this.config.allowedDays.push(6);

                    if ($this.config.selectDefaultDate && $this.config.defaultDate) {
                        $this.config.defaultDate.setDate($this.config.defaultDate.getDate() + 1);
                        var month = $this.config.defaultDate.getMonth() + 1,
                            day = $this.config.defaultDate.getDate(),
                            year = $this.config.defaultDate.getFullYear(),
                            date = month + '/' + day + '/' + year;
                        $this.datepicker("setDate", date);
                        $this.val(date);
                    }

                    $this.checkDate($this.val(), $this.config.specialDates);
                    $this.trigger("blur");
                }
                var args = {
                    target: $this,
                    connectFee: $this.config.StanderdConnectFee,
                };
                var switchTextSelectedEvent = $.Event("switchTextSelected", args);
                $(window).trigger(switchTextSelectedEvent);

                obj.append = $this.config.append;
                $.extend(opts, defaults, obj);
                return old.call($this, opts);
            }

            $this.resetCalendar = function () {
                $this.config.availableSaturday = false;
                $this.config.specialDates = {};
                $this.config.allowedDays = [1, 2, 3, 4, 5];
                $this.config.defaultDate = new Date();
            }

            $this.checkDate = function (dt, specDates) {

                var isValid = true;
                if (dt != "") {
                    var selectedDate = new Date(dt),
                        selYear = selectedDate.getFullYear(),
                        selMonth = selectedDate.getMonth() + 1,
                        selDay = selectedDate.getDate();

                    var messageContainer = $this.parents(".connectDatePicker").siblings(".connectDateMessage");
                    messageContainer.html("");

                    for (var key in specDates) {
                        var dateParts = key.split("-");
                        if (dateParts[0] == selYear && dateParts[1] == selMonth && dateParts[2] == selDay) {
                            switch (specDates[key]) {
                                case "priority":
                                    var args = {
                                        target: $this,
                                        connectFee: $this.config.priorityConnectFee,
                                        dateSelected: $.datepicker.formatDate("m/d/yy", selectedDate),
                                    };
                                    var priorityDateSelectedEvent = $.Event("priorityDateSelected", args);
                                    $(window).trigger(priorityDateSelectedEvent);
                                    break;
                                case "disabled":
                                    isValid = false;
                                    var args = {
                                        target: $this,
                                        dateSelected: $.datepicker.formatDate("m/d/yy", selectedDate),
                                        dateDisabled: obj.disabledDate,
                                    };
                                    var disabledDateSelectedEvent = $.Event("disabledDateSelected", args);
                                    $(window).trigger(disabledDateSelectedEvent);
                                    break;
                            }
                            break;
                        }
                    }

                    var dateAllowed = $.inArray(selectedDate.getDay(), $this.config.allowedDays) != -1;
                    if (!dateAllowed) {
                        messageContainer.html("<em class='error'>" + $.datepicker.formatDate("m/d/yy", selectedDate) + " " + unescape(obj.weekendDate) + "</em>");
                        isValid = false;
                    }
                }
                else
                {
                    isValid = false; 
                }
                return isValid;
            }

            if ($this.hasClass("newservice")) {

                var obj = $this.ParseClass();

                $this.config.allowedDays = obj.allowedDays || $this.config.allowedDays;
                $this.config.specialDates = obj.specDates || $this.config.specialDates;
                $this.config.availableSaturday = obj.availableSaturday || $this.config.availableSaturday;
                $this.config.selectDefaultDate = $this.hasClass("selectDefaultDate");

                opts = {
                    readonly: true,
                    handlerTimeout: 500000,
                    allowedDays: $this.config.allowedDays,
                    minDate: 0,
                    maxDate: "+90d",
                    hideIfNoPrevNext: true,
                    isValidDate: function() {
                        return $this.checkDate($this.val(), $this.config.specialDates);
                    },
                    beforeShowDay: function (d) {
                        var year = d.getFullYear(),
                            month = d.getMonth() + 1,
                            day = d.getDate();

                        for (var key in $this.config.specialDates) {
                            var datePart = key.split("-");
                            if (datePart[0] == year && datePart[1] == month && datePart[2] == day) {
                                if ($this.config.specialDates[key] == "disabled") return [0, ""];
                                return [1, $this.config.specialDates[key]];
                            }
                        }

                        var currentDate = new Date();
                        defaultDate = new Date();
                        maximumDate = $.datepicker._determineDate($this, opts.maxDate);

                        defaultDate.setDate($this.config.defaultDate.getDate() - 1);

                        if (defaultDate < currentDate) defaultDate.setDate(currentDate.getDate() - 1);
                        if (d < defaultDate || d > maximumDate) return [0, ""];
                        if ($.inArray(d.getDay(), $this.config.allowedDays) == -1) return [0, ""];

                        return [1, $this.config.openDateClass];
                    },
                    beforeShow: function (inp) {
                        jQuery(inp).select();
                    },
                    onClose: function (dt, input) {
                        $this.checkDate(dt, $this.config.specialDates);
                        jQuery(document).trigger("dpOnSelect");
                    },
                    onSelect: function (dt, input) {
                        $this.config.selectDefaultDate = false;
                        $this.checkDate(dt, $this.config.specialDates);
                        jQuery(document).trigger("dpOnSelect");
                    },
                    prepend: '<div class="header"><a href="javascript:void(0);" class="close">X</a></div>' + unescape(obj.prepend),
                    append: unescape(obj.append),
                    readonly: true
                };
            }

            if ($this.hasClass("addressConnectDates"))
            {
                $(window).on("invalidAddress", function (args) {
                    $this.datepicker("destroy");
                    $this.removeClass("valid invalid error");
                    $this.addClass("invalid-address");
                    $this.config.openDateClass = "ui-datepicker-invalid-address";
                    $this.resetCalendar();
                    $this.datepicker("refresh");
                });

                $(window).on("addressSelected", function (args) {

                    $this.removeClass("invalid-address invalid error valid").addClass("address-loading");

                    if (args.showLoading) ShowLoadingSpinner();
                    var success = false,
                        customerIntent = HttpHelper.getParameterByName("cint", "6"),
                        request = jQuery.getJSON("/Handlers/UpdateCalendarDates.ashx", { esiid: args.esiid, cint: customerIntent, calendar: "connect" },
                            function (data) {
                                if (data != null) {
                                    $this.datepicker("destroy");
                                    $this.update(data);
                                    $this.datepicker("refresh");
                                }
                                success = true;
                            });

                    setTimeout(function () {
                        if (!success) {
                            request.abort();
                        }
                    }, opts.handlerTimeout);

                    request.complete(function () {
                        $this.removeClass("address-loading");
                        if (args.showLoading) {
                            HideLoadingSpinner();
                        }
                    });
                });
            }

            $this.datepicker(opts);
        });
    };

    $(document).ready(function () {
        $.datepicker.init();
    });

})(jQuery);

1 个答案:

答案 0 :(得分:1)

尝试这样的事情......

 if (!$this.data("datepickerinit")) {
                    //    return;
                    //}

                    $this.data("datepickerinit", true);

                    this.isValidDate = function () {
                        return opts.isValidDate();
                    }
                } 

希望这对你有所帮助。祝你好运。