在回发后无法识别的选中项中启用禁用的复选框列表

Enabling disabled CheckBoxList in checked items not recognized after postback

本文关键字:启用 列表 复选框 识别      更新时间:2023-09-26

>我有一个CheckBoxList,它可以加载为禁用,但可能需要在某些客户端操作后启用。

我能够使用 jQuery 启用复选框,但问题是提交页面后,无法识别 ChceckBoxList 中的选定项(可能是因为控件不在 ViewState 中)。

该方案的简单示例:

<asp:CheckBoxList ID="chkList1" runat="server" Enabled="false" ClientIDMode="Static">
       <asp:ListItem Value="123" />
       <asp:ListItem Value="456" />
       <asp:ListItem Value="789" />
</asp:CheckBoxList>
$(document).ready(function()
{
    $("#divEnable").click(function()
    {
        var inputs = $("#chkList1").find("input");
        for (var i = 0; i < inputs.length; i++)
        {
            inputs[i].disabled = false;
        }
    });
}

然后在启用复选框后,选中它们并发送回发 - 无法识别任何选定的项目。

我尝试"手动"禁用这些项目,如

chkList1.Items[0].Attributes.Add("disabled", "disabled");

但这不起作用(disabled 属性附加到包含范围而不是输入控件)。

我还考虑过使用隐藏字段来跟踪复选框选择,但由于我在网格中有多个 CheckBoxLists,这将非常不优雅。

有什么办法吗?

试试这个:

$(document).ready(function()
{
    $("#divEnable").click(function()
    {
        var inputs = $("#chkList1").find("input")
            .each(function(){
                            //if($(this).is(":disabled"))
                            //{
                               $(this).prop("disabled",true);
                            //}
                          });      
});

裁判:
jQuery asp 控件 .prop("disabled", ") 在 IE 9
中未启用复选框无法处理禁用复选框

您可以使用

  for (i = 0; i < document.forms[0].length; i++)
   {
      e = document.forms[0].elements[i];
      if (e.id.indexOf("chklist") != -1)
      {
           e.disabled = false;
      }
  }

我遇到了同样的问题。我解决这个问题的方法是启用复选框列表启动,然后在加载页面时禁用使用脚本。这样,控件进入视图状态,但仍被禁用以启动。

    var checkBoxListToChange = document.getElementById('');
    var checkboxes = checkBoxListToChange.getElementsByTagName("input");
    for (var x = 0; x < checkboxes.length; x++) {
        checkboxes[x].disabled = true;
    }