我正在编写一个包含jQuery date picker的插件。我的PHP可以看到脚本但是抱怨一个函数(可能是jQuery冲突?)。每当页面加载时,我都会收到以下消息:
TypeError: undefined is not a function near '...jQuery(".date").datetimepicker...
四处搜索,我认为这可能是Wordpress中jQuery冲突的问题,但我觉得我现在正在黑暗中刺伤。
我的目录结构是:
trial_form.php:
<?php wp_deregister_script( 'jquery' ); ?>
<?php wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.js' ), false, NULL, true); ?>
<?php wp_enqueue_script( 'jquery' ); ?>
<?php wp_enqueue_script( 'script', plugins_url('calendar.js', __FILE__), array( 'jquery' )); ?>
<div id="trial-form">
<?php if ( $attributes['show_title'] ) : ?>
<h3><?php _e( 'Trial', 'personalize-login' ); ?></h3>
<?php endif; ?>
<div class="container">
<div class="row">
Choose the date for your free trial below!
</div>
<br />
<div class="row">
<div class='col-sm-3'>
<div class="form-group">
<div class='input-group date' id='datetimepicker'>
<input type='text' class="form-control" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
calendar.js:
jQuery(function () {
var bindDatePicker = function() {
jQuery(".date").datetimepicker({
format:'YYYY-MM-DD',
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down"
}
}).find('input:first').on("blur",function () {
// check if the date is correct. We can accept dd-mm-yyyy and yyyy-mm-dd.
// update the format if it's yyyy-mm-dd
var date = parseDate($(this).val());
if (! isValidDate(date)) {
//create date based on momentjs (we have that)
date = moment().format('YYYY-MM-DD');
}
$(this).val(date);
});
}
var isValidDate = function(value, format) {
format = format || false;
// lets parse the date to the best of our knowledge
if (format) {
value = parseDate(value);
}
var timestamp = Date.parse(value);
return isNaN(timestamp) == false;
}
var parseDate = function(value) {
var m = value.match(/^(\d{1,2})(\/|-)?(\d{1,2})(\/|-)?(\d{4})$/);
if (m)
value = m[5] + '-' + ("00" + m[3]).slice(-2) + '-' + ("00" + m[1]).slice(-2);
return value;
}
bindDatePicker();
});
请帮忙!