在我进行ajax调用并获得结果之后。我想将属性设置为可由下一个jquery console.log方法检索。不幸的是,这不起作用。
var other_month = $('#prev-month').attr('data-cur-month', "" + other_month);
$.post('../wp-content/custom/events/index.php', {
month: month,
year: year
},
function(data){
// refactor this so we can do this on the right side.
$('.event-wrapper-left').html($('.event-wrapper').html());
$('.event-wrapper').html($('.event-wrapper-right').html());
$('.event-wrapper-right').html(data);
$(self).html( monthNames[d.getMonth()]+ " " + d.getFullYear() + '<span class="glyphicon glyphicon-chevron-right"></span>');
$(self).attr('data-cur-month', "" + new_month);
$(self).attr('data-cur-year', "" + year);
$('#prev-month').html( '<span class="glyphicon glyphicon-chevron-left"></span>' + monthNames[other_date.getMonth()]+ " " + other_date.getFullYear());
console.log(other_month);
$('#prev-month').attr('data-cur-month', other_month);
// this come out as zero
console.log($('#prev-month').attr('data-cur-month'));
$('#prev-month').attr('data-cur-year', other_year);
HTML:
<a class="pull-left month-left month-changer" href="#" data-cur-year="<?= date('Y', strtotime('-1 month')) ?>" data-cur-month="<?= date('m', strtotime('-1 month')) ?>" id="prev-month"><span class="glyphicon glyphicon-chevron-left"></span><?= date('F Y', strtotime('-1 month')) ?></a>
<a class="pull-right month-right month-changer" href="#" data-cur-year="<?= date('Y', strtotime('+3 month')) ?>" data-cur-month="<?= date('m', strtotime('+3 month')) ?>" id="next-month"><?= date('F Y', strtotime('+3 month')) ?><span class="glyphicon glyphicon-chevron-right"></span></a>
答案 0 :(得分:0)
看起来使用ajax并填充属性不是最好的方法。如果使用ajax生成属性,dom将不会识别它在那里并且不知道如何访问它。
我所做的是重构我的解决方案,使用ajax函数之外的变量并从那里增加变量,而不是将变量存储在页面上的元素内。