我试图在ajax从服务器返回内容时检查复选框,但我不知道取消选中它们或替换dom内容(重置它们)...
}).done(function(data) {
if ($( '#search' ).is(':visible'))
$( '#search' ).hide();
if ($(this).is(':checkbox')) {
var new_content = $(data).find( '#scroll-to-list' );
$( '#scroll-to-list' ).replaceWith( new_content );
}
else {
var new_content = $(data).find( '#search-filters, #scroll-to-list' );
$( '#results' ).html( new_content );
}
$( 'html, body' ).animate({
scrollTop: $( '#scroll-to-list' ).offset().top
}, 1000);
});
搜索按钮触发on click事件并在dom中打印新div,隐藏#search div(和单选按钮,但仍会检查其中一个)。现在出现#search-filters div和复选框,当我检查其中一个调用ajax时,打印到dom的内容只是第二个div(包含搜索数据),但是复选框(来自#search-filters)是没有检查,我无法弄清楚导致这种情况的原因......
这是处理复选框的代码(在ajax之前):
$('body').on('click', '.click, :checkbox, .pag_link', function() {
if ($(this).is(':checkbox')) {
if ($(this).attr('class') == 'filter1' || $('.filter1').is(':checked')) {
if ($('.filter1').is(':checked'))
var type = $(this).val(); // maybe should be an array
else var type = null;
} else var type = null;
if ($(this).attr('class') == 'filter2' || $('.filter2').is(':checked')) {
if ($('.filter2').is(':checked'))
var status = $(this).val(); // maybe should be an array
else var status = null;
} else var status = null;
if ($(this).attr('class') == 'filter3' || $('.filter3').is(':checked')) {
if ($('.filter3').is(':checked'))
var bhk = $(this).val(); // maybe should be an array
else var bhk = null;
} else var bhk = null;
}
else {
var type = status = bhk = null;
}