我试图在选中或取消选中复选框时重新加载后保留复选框状态,以及用于选中和取消选中的 if 语句。我做错了什么或者最好的方法是什么?
$('.js-bbCheckbox').click(function(event) {
$('.js-bbCheckbox').change(function(){
if($(this).is(':checked')){
window.location = '?filter=topRated';
}else{
window.location = '?filter=nottopRated';
}
});
最佳答案
因此,您可以设置位置,但您需要页面根据过滤器更改复选框。这样,您就不需要保存 cookie。
$(document).ready(function () {
var topRated = new RegExp("[?|&]filter=topRated");
$('.js-bbCheckbox').prop('checked', function () {
var filter = window.location.search.match(topRated);
return filter != null;
}).change(function () {
window.location = $(this).is(':checked') ? '?filter=topRated' : window.location.toString().replace(topRated, "");
});
});
关于javascript - 重新加载页面后保留复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13036023/