触发单选按钮单击,并在页面刷新时记住选择


Trigger click on radio button with remembering selection on page refresh

这个问题不重复。

我有两个单选按钮(单选按钮 1、收音机2)。

我能够实现以下目标,但分别实现:

  1. 在页面加载时触发对"radio1"的点击。因此,每当加载页面时,都会单击该按钮。
  2. 请记住用户手动单击的所选单选按钮,使用Joseph Silber在此答案中描述的本地存储。因此,如果用户手动单击"radio2",然后刷新页面,它会记住该选择。

问题是我不能同时使用这两种方法。因为触发的单击始终会接管本地存储,这使得刷新页面时不会记住所选单选按钮。

默认情况下,我需要在页面加载时单击(未选中)"radio1",但是当用户手动单击"radio2"时,它会记住该选择,并且每当刷新页面时,"radio2"将相对于用户的选择被单击。

尝试了很多代码组合,我必须让它们一起工作,但我无法弄清楚。

用于在页面加载时触发单击单选按钮的代码:

<script type="text/javascript">
$(document).ready(function() {
  $("#radio1 input:radio").click(function() {
    alert("clicked");
   });
  $("input:radio:first").prop("checked", true).trigger("click");
});
</script>

本地存储的代码;记住无线电选择:

<script type="text/javascript">
$(function()
{
    $('input[type=radio]').each(function()
    {
        var state = JSON.parse( localStorage.getItem('radio_'  + this.id) );
        if (state) this.checked = state.checked;
    });
});
$(window).bind('unload', function()
{
    $('input[type=radio]').each(function()
    {
        localStorage.setItem(
            'radio_' + this.id, JSON.stringify({checked: this.checked})
        );
    });
});
</script>
同样,它们都可以

正常工作,但分开,它们中的任何一个都可以工作。

如果有人能帮助我使这两种方法一起工作,我将不胜感激。

为什么不只设置默认的无线电:checked属性,而是设置.trigger('click')

$(function(){
    //state default radio ':checked' property there:
    $("input:radio:first").prop("checked", true);
    $("#radio1 input:radio").click(function() {
        alert("clicked");
    });
    // overwrite radio ':checked' property there (if exists in localStorage) :
    $('input[type=radio]').each(function(){
        var state = JSON.parse( localStorage.getItem('radio_'  + this.id) );
        if (state) this.checked = state.checked;
    });
});
$(window).bind('unload', function(){
    $('input[type=radio]').each(function(){
        localStorage.setItem(
            'radio_' + this.id, JSON.stringify({checked: this.checked})
        );
    });
});

JSFiddle


或者,如果您需要在 DOM ready 上触发默认无线电的事件处理程序(就像您在代码中使用 .trigger('click') 所做的那样),请使用以下命令:

$(function(){
    //state default radio ':checked' property there and run its `click` event handler:
    radioClick.call($("input:radio:first").prop("checked", true));
    $("#radio1 input:radio").click(radioClick);
    // method triggered on radio click, will skip the localStorage modification:
    function radioClick() {
        // you can refer here to 'this' as to clicked radio
        alert($(this).val());
    }
    // overwrite radio ':checked' property there (if exists in localStorage) :
    $('input[type=radio]').each(function(){
        var state = JSON.parse( localStorage.getItem('radio_'  + this.id) );
        if (state) this.checked = state.checked;
    });
});
$(window).bind('unload', function(){
    $('input[type=radio]').each(function(){
        localStorage.setItem(
            'radio_' + this.id, JSON.stringify({checked: this.checked})
        );
    });
});

JSFiddle


或者,更好的是,在 HTML 中设置默认的无线电checked属性权限:

<input type=radio id=radio1 value=radio1 name=radio checked />
<input type=radio id=radio2 value=radio2 name=radio />
<input type=radio id=radio3 value=radio3 name=radio />
...

这样您就可以以本机方式而不是以编程方式checked拥有它。然后覆盖其prop取决于localStorage中的内容


编辑(对答案的OP注释)

$(function () {
    $('input[type="radio"]').click(function () {
        localStorage.setItem('radioIdSelected', $(this).attr('id'));
        // your method to run on selected radio button (alert for demo):
        alert($(this).attr('id'));
    });
    var storageRadio = localStorage.getItem('radioIdSelected');
    if (storageRadio !== null && storageRadio !== undefined && $('#' + storageRadio).length) {
        $('#' + storageRadio).trigger('click');
    } else {
        $('input[type="radio"]:first').trigger('click');
    }
});

JSFiddle