以下代码在事件开始时计算一个人的年龄。它具有来自wordpress插件的一些短代码。年龄计算正确,但日期选择器未显示用于选择年份和月份的下拉菜单。小日历图标也不会显示。这是代码:
$( document ).ready( function(){
// Edit fields as required
var dateField = 'geburts-datum';
var ageField = 'alter';
var eventSelector = '#veranstaltungs-beginn';
var toId = $('input[name="wpcf-' + dateField + '[display-only]"]').attr('id');
$( "#"+toId ).datepicker( {onSelect: function( dateText, instance){
if ( instance.currentYear == 0 ) return;
// get event start date
var eventStart = $( eventSelector ).text();
var eventStartInt = parseInt(eventStart);
var eventDate = new Date( eventStartInt * 1000 );
console.log("Event-Date: " + eventDate);
var eventYear = eventDate.getFullYear();
var eventMonth = eventDate.getMonth();
var eventDay = eventDate.getDay();
var age = eventYear - instance.selectedYear;
if ( instance.selectedMonth > eventMonth ) age--;
else {
if ( instance.selectedMonth == eventMonth ) {
if ( instance.selectedDay > eventDay ) age--;
}
}
// update target field with age
$('input[name="wpcf-' + ageField + '"]' ).val( age );
}
});
});
但是,如果我在“ $('input [name =“ wpcf-'+ ageField +'”]')。val(age);”之后删除“}”,则日期选择器会正确显示,但控制台会显示出现语法错误,计算将不再起作用。有什么想法,代码有什么问题吗? Datepicker does not show correctly