简体   繁体   English

页面刷新后保留选中多个单选按钮

[英]Retain Multiple Radio Buttons checked after page refresh

In a form there are 2 group of radio buttons. 在一种形式中,有2组单选按钮。

In the first group named Result, there are 4 option: id="ok", id="fa", id="fp", id="bp". 在名为Result的第一个组中,有4个选项:id =“ ok”,id =“ fa”,id =“ fp”,id =“ bp”。

In the second group named ResultCategories, there are 9 option: id="cat1" .... id="cat9". 在第二个名为ResultCategories的组中,有9个选项:id =“ cat1” .... id =“ cat9”。

What I want: 我想要的是:

a. 一种。 If ok is clicked, ResultCategories will be unchecked (if already checked). 如果单击“确定”,则ResultCategories将被取消选中(如果已选中)。

b. If fp or bp is clicked, cat9 of ResultCategories will be checked. 如果单击fp或bp,将检查ResultCategories的cat9。

c. C。 If one of the cat1 to cat8 of ResultCategories is clicked, fa of Result will be checked. 如果单击ResultCategories的cat1到cat8之一,将检查Result的fa。

d. d。 When radio buttons are clicked page will refresh and checked radio buttons remain checked. 单击单选按钮时,页面将刷新,并且选中的单选按钮保持选中状态。

I got a to c working but d is not working . 我到c工作,但d没有工作 It retains checked radio for only one group. 它仅保留一组的选中的无线电。

Here is what tried: 这是尝试的方法:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
    <div class="container">
        <form action="" method="POST">
            <fieldset class="scheduler-border">
                <legend class="scheduler-border">Quality Check</legend>
                <div style="float: right;">
                    <button type="submit" id="submit" name="submit" class="btn btn-info">Complete</button>
                </div>
                <br />
                <br />
                <br />
                <div class="row">
                    <div class="column">
                        <div id="Result">
                            <label>Result:</label>
                            <label class="radioContainer">Ok 

                                <input type="radio" name="Result" id ="ok" value="1" />
                                <span class="circle"></span>
                            </label>
                            <label class="radioContainer">Fasle Alarm 
                                <input type="radio" name="Result" id="fa" value="2" />
                                <span class="circle"></span>
                            </label>
                            <label class="radioContainer">False Pass 
                                <input type="radio" name="Result" id="fp" value="3" />
                                <span class="circle"></span>
                            </label>
                            <label class="radioContainer">Blatant Pass 
                                <input type="radio" name="Result" id="bp" value="4" />
                                <span class="circle"></span>
                            </label>
                        </div>
                    </div>
                    <br />
                    <div class="column">
                        <div id="ResultCategories">
                            <label>Result Categories:</label>
                            <div>
                                <label class="radioContainer">Cat 1 
                                    <input type="radio" name="ResultCategories" id="cat1" value="1" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 2 
                                    <input type="radio" name="ResultCategories" id="cat2" value="2" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 3 
                                    <input type="radio" name="ResultCategories" id="cat3" value="3" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 4 
                                    <input type="radio" name="ResultCategories" id="cat4" value="4" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 5 
                                    <input type="radio" name="ResultCategories" id="cat5" value="5" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 6 
                                    <input type="radio" name="ResultCategories" id="cat6" value="6" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 7 
                                    <input type="radio" name="ResultCategories" id="cat7" value="7" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 8 
                                    <input type="radio" name="ResultCategories" id="cat8" value="8" />
                                    <span class="circle"></span>
                                </label>
                                <label class="radioContainer">Cat 9 
                                    <input type="radio" name="ResultCategories" id="cat9" value="9" />
                                    <span class="circle"></span>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>
</body>
<script>   $('input:radio').click(function() {
        location.reload();   }); </script>

I have added a fiddle here: Fiddle 我在这里添加了一个小提琴: 小提琴

$(document).ready(function() {

  var result_dom = $('input[name="Result"]');
  var categories_dom = $('input[name="ResultCategories"]');
  var cat9 = $('#cat9');
  var fa = $('#fa')

  result_dom.on('change', function() {
    var checked_val = $(this).val();
    if (checked_val == 1) {
      categories_dom.prop('checked', false);
    } else if (checked_val == 3 || checked_val == 4) {
      cat9.prop('checked', true);
    }
  });

  categories_dom.on('change', function() {
    var checked_val = $(this).val();
    if (checked_val >= 1 && checked_val <= 8) {
      fa.prop('checked', true);
    }
  });

});

$(document).ready(function(){
  if(localStorage.selected) {
    $('#' + localStorage.selected ).attr('checked', true);
  }
  $('.radio').click(function(){
    localStorage.setItem("selected", this.id);
  });
});

How can I retain both group of radio buttons checked? 如何保留选中的两组单选按钮?

Updated Code: 更新的代码:

<head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
      <script>
          $(document).ready(function() {

        var result_dom = $('input[name="Result"]');
        var categories_dom = $('input[name="ResultCategories"]');
        var cat9 = $('#cat9');
        var fa = $('#fa')

        result_dom.on('change', function() {
        var checked_val = $(this).val();
        if (checked_val == 1) {
          categories_dom.prop('checked', false);
        } else if (checked_val == 3 || checked_val == 4) {
          cat9.prop('checked', true);
        }
        });

        categories_dom.on('change', function() {
        var checked_val = $(this).val();
        if (checked_val >= 1 && checked_val <= 8) {
          fa.prop('checked', true);
        }
        });

        });$(document).ready(function() {

        var result_dom = $('input[name="Result"]');
        var categories_dom = $('input[name="ResultCategories"]');
        var cat9 = $('#cat9');
        var fa = $('#fa')

        result_dom.on('change', function() {
        var checked_val = $(this).val();
        if (checked_val == 1) {
          categories_dom.prop('checked', false);
        } else if (checked_val == 3 || checked_val == 4) {
          cat9.prop('checked', true);
        }
        });

        categories_dom.on('change', function() {
        var checked_val = $(this).val();
        if (checked_val >= 1 && checked_val <= 8) {
          fa.prop('checked', true);
        }
        });

        });

        $(document).ready(function(){
          //get the selected radios from storage, or create a new empty object
          var radioGroups = JSON.parse(localStorage.getItem('selected') || '{}');

          //loop over the ids we previously selected and select them again
          Object.values(radioGroups).forEach(function(radioId){
            document.getElementById(radioId).checked = true;
          });

          //handle the click of each radio
          $('.radio').on('click', function(){
            //set the id in the object based on the radio group name
            //the name lets us segregate the values and easily replace
            //previously selected radios in the same group
            radioGroups[this.name] = this.id;
            //finally store the updated object in storage for later use
            localStorage.setItem("selected", JSON.stringify(radioGroups));
          });
        });
      </script>
    </head>

    <body>
      <div class="container">
        <form action="" method="POST">
          <fieldset class="scheduler-border">
            <legend class="scheduler-border">Quality Check</legend>
            <div style="float: right;"><button type="submit" id="submit" name="submit" class="btn btn-info">Complete</button></div>
            <br /> <br /> <br />
            <div class="row">
              <div class="column">
                <div id="Result">
                  <label>Result:</label>
                    <label class="radioContainer">Ok
                    <input class="radio" type="radio" name="Result" id ="ok" value="1">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Fasle Alarm <input class="radio" type="radio" name="Result" id="fa" value="2">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">False Pass <input class="radio" type="radio" name="Result" id="fp" value="3" >
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Blatant Pass <input class="radio" type="radio" name="Result" id="bp" value="4">
                    <span class="circle"></span>
                  </label>
                </div>
              </div>
              <br />
              <div class="column">
                <div id="ResultCategories"><label>Result Categories:</label>
                  <div>
                    <label class="radioContainer">Cat 1 <input class="radio" type="radio" name="ResultCategories" id="cat1" value="1">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Cat 2 <input class="radio" type="radio" name="ResultCategories" id="cat2" value="2">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Cat 3 <input class="radio" type="radio" name="ResultCategories" id="cat3" value="3">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Cat 4 <input class="radio" type="radio" name="ResultCategories" id="cat4" value="4">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Cat 5 <input class="radio" type="radio" name="ResultCategories" id="cat5" value="5">
                    <span class="circle"></span>
                    </label> <label class="radioContainer">Cat 6 <input class="radio" type="radio" name="ResultCategories" id="cat6" value="6">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Cat 7 <input class="radio" type="radio" name="ResultCategories" id="cat7" value="7">
                    <span class="circle"></span>
                    </label> <label class="radioContainer">Cat 8 <input class="radio" type="radio" name="ResultCategories" id="cat8" value="8">
                    <span class="circle"></span>
                    </label>
                    <label class="radioContainer">Cat 9 <input class="radio" type="radio" name="ResultCategories" id="cat9" value="9">
                    <span class="circle"></span>
                    </label>
                  </div>
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </body>
    <script>
      $('input:radio').click(function() {
        location.reload();
      });
    </script>

 $(document).ready(function(){ //get the selected radios from storage, or create a new empty object var radioGroups = JSON.parse(localStorage.getItem('selected') || '{}'); //loop over the ids we previously selected and select them again Object.values(radioGroups).forEach(function(radioId){ document.getElementById(radioId).checked = true; }); //handle the click of each radio $('.radio').on('click', function(){ //set the id in the object based on the radio group name //the name lets us segregate the values and easily replace //previously selected radios in the same group radioGroups[this.name] = this.id; //finally store the updated object in storage for later use localStorage.setItem("selected", JSON.stringify(radioGroups)); }); }); 

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

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