繁体   English   中英

如何设置刷新后选中的复选框?

[英]How do i set the checkbox item checked after refresh?

我有一个dropdowncheckbox和hiddenfield值。隐藏字段的值是1,3、4、5,所以我想设置dropdowncheckbox在jquery中回发后检查相关的hiddenfield值,该怎么做?

$(document).ready(function() {
    var Statushdn = document.getElementById('<%= hdnSubCategoryId.ClientID%>').value;
    var str_array = Statushdn.split(',');
    var summar;
    $.each($('#ContentPlaceHolder1_ddcbProductStockSubCategory_dv input[type=checkbox]:not(:checked)'), function() {

        summar = $(this).val();
        for (var i = 0; i < str_array.length; i++) {

            if (str_array[i] == summar)
                $(this).attr('checked', 'checked');
        }
    });
});

您可以使用localstorage保留复选框中的值,例如:

$(':checkbox').on('change', function () {
    //set the check value of checkbox
    localStorage.setItem(this.id, this.checked);
});

$(':checkbox').each(function () {   
    //retrieve the checked value from the checkboxes and 'convert' it to bool
    var status = localStorage.getItem(this.id) === "false" ? false : true;
    $(this).prop("checked", status);
});

<input type="checkbox" id="chk1" />
<input type="checkbox" id="chk2" />
<input type="checkbox" id="chk3" />
<input type="checkbox" id="chk4" />

小提琴

参考文献:

Window.localStorage

暂无
暂无

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

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