【问题标题】:How do i set the checkbox item checked after refresh?如何设置刷新后选中的复选框项目?
【发布时间】:2015-07-20 11:37:27
【问题描述】:

我有一个 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');
        }
    });
});

【问题讨论】:

  • 一个cookie或在查询字符串中传递它,除了需要服务器端语言。
  • 你在使用更新面板吗??
  • 将所需的值存储在会话中,然后在整个应用程序中调用该会话值,直到该会话值被清除或更改为止。

标签: javascript jquery asp.net webmethod hidden-field


【解决方案1】:

您可以使用 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" />

fiddle

参考资料:

Window.localStorage

【讨论】:

  • 实际上我有隐藏字段中的值。但我希望将这些隐藏字段值设置到回发后选中的复选框中。
  • 但是隐藏字段在回发后会丢失它们的值,或者您从数据库中检索它们?
  • 但是当我在控制台中调试 javascript 时,它们会检索隐藏文件的值。但是 $.each(function() 中存在一些问题。请查看我上面的代码,如果您有任何想法,请告诉我改正。
猜你喜欢
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多