在Wordpress中,我试图用Ajax编写日历,以便在更改月份时进行刷新。我的问题是:我只能更改月份一次,之后,当我单击链接更改月份时,似乎什么都没有发生。
您可以在此处看到:https://www.bons-plans-voyage-new-york.com/backend/calendrier/
我的代码:
function calendar_enqueue_scripts()
{
if(!is_admin())
{
if(is_home() || is_page("calendrier"))
{
wp_deregister_script(\'jquery\');
wp_enqueue_script(\'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js\', array(), null, true);
wp_enqueue_script(\'calendrier\', get_template_directory_uri().\'/js/calendrier.js\', array(\'jquery\') );
wp_localize_script(\'calendrier\', \'ajax_object\', array(
\'ajax_url\' => admin_url( \'admin-ajax.php\' ),
\'nonce\' => wp_create_nonce(\'my_nonce\')
));
}
}
}
add_action(\'wp_enqueue_scripts\', \'calendar_enqueue_scripts\');
function my_action()
{
if ( isset( $_POST[\'year\']) && isset($_POST[\'month\']) )
{
check_ajax_referer( \'my_nonce\', \'nonce\' ); // This function will die if nonce is not correct.
$calendrier = get_html_calendar($_POST[\'year\'],$_POST[\'month\']);
echo $calendrier;
exit();
}
}
add_action( \'wp_ajax_my_action\', \'my_action\' );
add_action( \'wp_ajax_nopriv_my_action\', \'my_action\' );
function get_html_calendar($year = \'\',$month = \'\')
{
// some stuff to generate calendar for given month & year
$output = \'\';
$output .= \'<div id="calender_section" class="cf">\';
$output .= \'<h2>\';
$output .= \'<a year="\'.date("Y",strtotime($date.\' - 1 Month\')).\'" month="\'.date("m",strtotime($date.\' - 1 Month\')).\'"><<</a>\';
$output .= \'<select name="month_dropdown" class="month_dropdown dropdown">\'.getAllMonths($dateMonth).\'</select>\';
$output .= \'<select name="year_dropdown" class="year_dropdown dropdown">\'.getYearList($dateYear).\'</select>\';
$output .= \'<a year="\'.date("Y",strtotime($date.\' + 1 Month\')).\'" month="\'.date("m",strtotime($date.\' + 1 Month\')).\'">>></a>\';
$output .= \'</h2>\';
// ... etc
return $output;
}
还有我的日历。js公司:
(function($)
{
function ajax_refreshCalendar(target_div, year, month)
{
$.ajax({
url : ajax_object.ajax_url, // Note that \'ajax_object\' is from the wp_localize_script() call.
type : \'post\',
data :
{
action : \'my_action\', // Note that this is part of the add_action() call.
nonce : ajax_object.nonce, // Note that \'aj_demo_nonce\' is from the wp_localize_script() call.
year : year,
month : month,
},
success : function( response )
{
console.log("success");
console.log(response);
$("#"+target_div).html(response);
}
}).fail(function (response)
{
// This stuff only happens if things fail miserably.
console.log("fail");
console.log(response);
});
}
$(document).ready(function()
{
// Detect clics
$("#calender_section a").on(\'click\', function(e)
{
console.log("clic");
// Prevent them from actually visiting the URL when clicking.
e.preventDefault();
var year = $(this).attr(\'year\');
var month = $(this).attr(\'month\');
var target_div = \'calendrier_wrapper\';
ajax_refreshCalendar(target_div, year, month);
});
});
})(jQuery);
我错过了什么?**编辑:更奇怪的是,我得到了不同的结果,这取决于我是否登录。我在这里迷路了。