简体   繁体   English

从第2页退回到第1页时如何选中第1页中的复选框,并在刷新页面后清除选中的按钮

[英]How to keep checkbox in page 1 checked when click back from page 2 to page 1, and clear the checked button after refresh page

Firstly, I want to keep my checkbox in page 1 checked when I back to page 1 From page 2. Then, I want to make all checked checkbox to be unchecked when I refresh the page. 首先,当我从页面2回到页面1时,我希望保持页面1中的复选框处于选中状态。然后,当我刷新页面时,我想取消选中所有复选框。

I have done the first part, which is keep the checkbox checked. 我已经完成了第一部分,即保持复选框处于选中状态。 But I cannot clear the checked checkbox after I refresh page. 但是刷新页面后,我无法清除已选中的复选框。 Any suggestions? 有什么建议么?

Here I provide my HTML and jQuery code of page 1: 在这里,我提供了第1页的HTML和jQuery代码:

 .on('click', '#btnCancel', function() { $('#indexContent').show(); $('#mainContent').empty(); $.each(radioValues, function(key, value) { $("#" + key).prop('checked', false); }); $.each(checkboxValues, function(key, value) { $("#" + key).prop('checked', false); }); }) var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {}, $checkboxes = $("#myTable :checkbox"); $checkboxes.on("change", function(){ $checkboxes.each(function(){ checkboxValues[this.id] = this.checked; }); localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues)); }); // On page load $.each(checkboxValues, function(key, value) { $("#" + key).prop('checked', value); }); var radioValues = JSON.parse(localStorage.getItem('radioValues')) || {}, $radios = $("#myTable :radio"); $radios.on("change", function(){ $radios.each(function(){ radioValues[this.id] = this.checked; }); localStorage.setItem("radioValues", JSON.stringify(radioValues)); }); // On page load $.each(radioValues, function(key, value) { $("#" + key).prop('checked', value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="striped" id="myTable"> <thead> <tr> <th>Primary</th> <th>Tag / Untag</th> <th>Accounts</th> </tr> </thead> <tbody> <tr> <td><input class="with-gap" name="group1" type="radio" id="tes1" value="1" /> <label for="tes1"></label></td> <td><input type="checkbox" class="filled-in" id="filled-in-box1" value="1" /> <label for="filled-in-box1"></label></td> <td><span class="account" id="acc1">123456789</span></td> </tr> <tr> <td><input class="with-gap" name="group1" type="radio" id="tes2" value="2" /> <label for="tes2"></label></td> <td><input type="checkbox" class="filled-in" id="filled-in-box2" value="2" /> <label for="filled-in-box2"></label></td> <td><span class="account" id="acc2">147852369</span></td> </tr> </tbody> </table> <div class="col-button form-footer"> <button id="btnCancel" type="button" name="action" > Cancel </button> <button id="btnNext" type="button" name="action"> Next </button> </div> 

Hi my friend. 你好我的朋友。

Try this in your btnCancel function: 在您的btnCancel函数中尝试以下操作:

$("#btnCancel").click(function(){
    $('#indexContent').show();
    $('#mainContent').empty();
      $('input[type=checkbox]').prop('checked',false);
      $('input[type=radio]').prop('checked',false);
});

Here is the example code: 这是示例代码:

https://jsfiddle.net/sosymhuj/1/ https://jsfiddle.net/sosymhuj/1/

I am not sure if you missed pasting some code but your first script line ( .on('click' ... ) is not valid. 我不确定您是否错过了粘贴某些代码的步骤,但是您的第一个脚本行( .on('click' ... )无效。

Change it to 更改为

$('#btnCancel').on('click', function() {
            $('#indexContent').show();
            $('#mainContent').empty();
            $.each(radioValues, function(key, value) {
                $("#" + key).prop('checked', false);
            });
            $.each(checkboxValues, function(key, value) {
                $("#" + key).prop('checked', false);
            });
        })  

and it should work 它应该工作

Here is the according JSFiddle 是根据JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM