我有多个按钮用于日期过滤。我正在根据所选按钮显示记录。现在,我想将bootstrap活动类添加到按钮(如果单击)。否则,它将不会处于活动状态,如果我们单击另一个按钮,则该按钮将处于活动状态,然后将删除上一个按钮的活动类。以下是我的按钮:-
<div class="top_stats_wrapper">
<div class="right-side">
<form method="get" >
<input type="hidden" name="time" value="today" />
<button type="button" class="btn btn-info mright5 test pull-left display-block" id="today" onclick="showAll('today')">Today</button>
</form>
<form method="get" >
<input type="hidden" name="time" value="week" />
<button type="button" class="btn btn-info mright5 test pull-left display-block " id="week" onclick="showAll('week')">This Week</button>
</form>
</div>
</div>
以下我尝试添加活动类的代码:-
function showAll(time = '', from_date = '', to_date = ''){
$.ajax({
type: 'ajax',
datatype: 'json',
data: {time:time,from_date:from_date,to_date:to_date},
url: '<?php echo site_url('admin/home/top_stats') ?>',
method: 'GET',
success: function (data)
{
$('#top_stats').html(data);
if($("#today").hasClass("active")){
$("#today").removeClass("active");
}else {
$("#today").addClass("active");
}
}
});
}