Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

javascript - JS/jQuery TypeError: jQuery(...).datepicker is not a function

I've been scratching my head on this for 2 days, pretty sure I'm just missing something simple but I can't for the life of me figure out why it's not working.

I'm trying to use the script below on my WordPress site to disable specific dates within a datepicker field in a ContactForm7 form.

I can load the script in jsfiddle with a simple input field using the same id and it works perfectly...but when I add it to my site the dates aren't disabled, and there's an error in the JS error console that says "jQuery(...).datepicker is not a function"

I've added it in my header.php file, just below the wp_head() call and just above the </head> tag. I've assigned my datepicker field with the id of dpick like the script uses.

I read that this error is commonly caused when using the $ symbol because it can conflict with other jQuery scripts within WordPress...so they suggested replacing $ with jQuery instead (which I've done in the script below)...but I'm still getting the error

var unavailableDates = ["1-9-2013", "2-9-2013", "3-9-2013", "4-9-2013", "5-9-2013"];

    function unavailable(date) {
            dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
            if (jQuery.inArray(dmy, unavailableDates) == -1) {
                return [true, ""];
            } else {
                return [false, "", "Unavailable"];
        }
    }

        jQuery(function() {
            jQuery( '#dpick' ).datepicker({
                dateFormat: 'dd MM yy',
                beforeShowDay: unavailable
        });

    });

Can't thank you enough for any help you can offer...this seems like such a simple thing but I just can't seem to wrap my head around it!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The are several reasons for this error:

  1. The jquery.ui is used before jquery.
  2. The $ is used by another library.
  3. The jquery lib that is referenced locally(wordpress) has different version from that using jquery.ui.
  4. When the right library and version is referenced the browser cache must be cleared.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...