我的rails应用程序中有一些复选框,代码在下面
<%= check_box_tag "audio_ids[]",audio.id ,nil,class: "song"%>
<%= check_box_tag "audio_ids[]",audio.id ,nil,class: "song"%>
<%= check_box_tag "audio_ids[]",audio.id ,nil,class: "song"%>
<button class="btn btn-warning">play all</button>
<script>
function handleButtonClick(button){
if ($(button).text().match("Check all")){
$(":checkbox").prop("checked", true)
} else {
$(":checkbox").prop("checked", false)
};
updateButtonStatus();
}
function updateButtonStatus(){
var allChecked = $(":checkbox").length === $(":checkbox:checked").length;
$("button").text(allChecked? "Uncheck all" : "Check all");
}
function updateCookie(){
var elementValues = {};
$(":checkbox").each(function(){
elementValues[this.id] = this.checked;
});
elementValues["buttonText"] = $("button").text();
$.cookie('elementValues', elementValues, { expires: 7, path: '/' })
}
function repopulateFormELements(){
var elementValues = $.cookie('elementValues');
if(elementValues){
Object.keys(elementValues).forEach(function(element) {
var checked = elementValues[element];
$("#" + element).prop('checked', checked);
});
$("button").text(elementValues["buttonText"])
}
}
$(":checkbox").on("change", function(){
updateButtonStatus();
updateCookie();
});
$("button").on("click", function() {
handleButtonClick(this);
updateCookie();
});
$.cookie.json = true;
repopulateFormELements();
</script>
他们只是刷新页面时保持状态,但每当我来自不同的页面时,他们就会失去状态,但是当我单独检查这段代码时,它可以工作但不能使用rails,这段代码有什么问题我需要做什么?