我还是 JS 的初学者。
我使用 ajax 来过滤 wordpress 中的内容。所以我需要向 PHP 发送 2 个变量。但问题是,一旦我进一步导航,其中一个变量就需要改变。在 html 中,数据被更改,但在 (document).ready 上运行的 jquery 使用初始变量。如何使 ajax 上的变量刷新完成?
这是我的代码
jQuery(document).ready(function($) {
$('.tax-filter').click( function(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
var selecetd_taxonomy = $(this).attr('id');
var personData = $('#content').data('id');
data = {
action: 'filter_posts',
afp_nonce: afp_vars.afp_nonce,
taxonomy: selecetd_taxonomy,
person: personData,
};
$.ajax({
type: 'post',
dataType: 'json',
url: afp_vars.afp_ajax_url,
data: data,
success: function( data, textStatus, XMLHttpRequest ) {
$('.showContent').html( data.response );
},
error: function( MLHttpRequest, textStatus, errorThrown ) {
$('.projects').html( 'Error 404' );
}
})
});
});
html 中的 .data('id') 从另一个函数获取了一个新值,但我也需要 jQuery 来获取新值
编辑:
我需要在我的点击函数中更新 personData。 #content 的 data-id 值在 ajaxComplete 上更新。它由另一个从 PHP 获取值的点击函数完成。
最佳答案
使用 $('#content').data('id', 'newValueHere')
代替 $('#content').attr(' data-id', 'newValue')
参见 fiddle请注意,使用 .attr() 更改属性不起作用,但 .data() 可以。
关于javascript - 在 ajax 完成时更新 jQuery 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798110/