Reset checkbox checked state go back from history

后端 未结 4 491
余生分开走
余生分开走 2021-01-13 01:15

In the following example, I use checkbox for making a pure CSS dropdown navigation, also available in this jsFiddle example.

Open that fiddle, click

相关标签:
4条回答
  • 2021-01-13 01:24

    Further to the other comments, if you are writing an AJAX driven page that uses pushState/replaceState, and the back button would not actually reload a page (i.e. no HTTP request), you can listen for the popState event and clear the checkbox.

    window.onpopstate = function(event) {
       $("#m").prop('checked',false );
    };
    

    It's a HTML 5 feature that's not fully supported across all browsers (although support is pretty good)

    https://developer.mozilla.org/en-US/docs/Web/Events/popstate

    Other options would be to use libraries like history.js, jquery-bbq, sammyjs, and so on.

    0 讨论(0)
  • 2021-01-13 01:34

    No need for JS or CSS, just add autocomplete="off" to the checkbox. This will prevent the browser from caching the 'checked' status.

    Example: https://jsfiddle.net/6g5u8wkb/

    Also, if you have multiple checkboxes, I beleive you can add autocomplete="off" to the form element to apply the effect to all inputs fields.

    0 讨论(0)
  • 2021-01-13 01:43

    Try adding something like this:

    $( document ).ready(function() {
      $("#m").prop('checked',false );
    });
    

    $( document ).ready(function() {
      $("#m").prop('checked',false );
    });
    #n {
      display: none;
    }
    #m:checked ~ #n {
      display: block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <label for="m">Menu</label>
    <input id="m" type="checkbox">
    <nav id="n">
      <ul>
        <li><a href="//example.com">Link 1</a></li>
      </ul>
    </nav>

    0 讨论(0)
  • 2021-01-13 01:46

    add autocomplete="off" to the checkbox.

    0 讨论(0)
提交回复
热议问题