0

我有一个动态生成的 asp.net 复选框列表,如果复选框最初被选中并且未选中,我需要处理一些代码。

这需要在 Jquery 或 Javascript 中完成。

我看到的所有示例都处理获取“已检查”的所有值我只对获取触发 onClick() 事件 ID 的特定项目感兴趣,以便我可以运行一些其他代码来处理它。

public void DrawPoolWeekCbList()
{
    var poolWeeks = ControllerFacade.GetAllPoolWeeksByPoolYear(PageView.PoolWeekYear.Text);
    var pWeekCount = poolWeeks.Count;
    var curRecord = 0;
    foreach (var poolWeek in poolWeeks)
    {
        PageView.cblPoolWeeks.Items.Add(poolWeek);
        PageView.cblPoolWeeks.Items[curRecord].Attributes.Add("onclick", "OnCheckBoxClicked("+ poolWeek +")");
        curRecord++;
    }
}

//UI Code
function OnCheckBoxClicked(poolWeek) {
    var chkId = poolWeek.toString();
    $('#<%= lblPoolWeekChange.ClientID %>').text(chkId);
    $(".addNew").dialog("open");
}

我见过的所有示例都找到了所有选中的复选框并循环遍历它们,我再次只想要触发该功能的特定复选框 id。

谢谢!

//Generated HTML - 
<input id="ContentPlaceHolder1_cblPoolWeeks_0" type="checkbox" name="ctl00$ContentPlaceHolder1$cblPoolWeeks$0" onclick="OnCheckBoxClicked(01);" value="01" /><label for="ContentPlaceHolder1_cblPoolWeeks_0">01</label></td><td><input id="ContentPlaceHolder1_cblPoolWeeks_1" type="checkbox" name="ctl00$ContentPlaceHolder1$cblPoolWeeks$1" onclick="OnCheckBoxClicked(02);" value="02" /><label for="ContentPlaceHolder1_cblPoolWeeks_1">02</label></td><td><input id="ContentPlaceHolder1_cblPoolWeeks_2" type="checkbox" name="ctl00$ContentPlaceHolder1$cblPoolWeeks$2" onclick="OnCheckBoxClicked(03);" value="03" /><label for="ContentPlaceHolder1_cblPoolWeeks_2">03</label>
4

1 回答 1

1

这个片段对我有用,请记住,asp.net 页面上只有一个 CheckBoxList,如果有多个这可能不起作用 - 请记住,这是用于处理单个复选框,而不是用于迭代,如“检查所有”功能。如果您需要将所有检查值添加到会话变量(我的情况)以供以后更新记录,这很方便。

   $(document).ready(function () {
       $("input:checkbox").click(function () {
           if ($(this).is(":checked")) {
               OnCheckBoxClicked(this.value);
           } else {
               OnUnchecked(this.value);
           }
       });
   }); 

   function OnCheckBoxClicked(poolWeek) {

       var chkId = poolWeek.toString();
       //Show DialogBox - Set label to pool week.
       $('#<%= lblPoolWeekChange.ClientID %>').text(chkId);
       $(".addNew").dialog("open");

   }

   function OnUnchecked(poolWeek) {
       var chkId = poolWeek.substring(2, 4);
       var commodity = $('#<%= Commodity.ClientID %> option:selected').text();
       $.ajax(
                {
                    type: "POST",
                    url: "Ajax/DeleteExcludedPoolWeek.aspx",
                    data: "poolWeek=" + chkId +  "&commodity=" + commodity,
                    success: function (msg) {
                        $('#myDiv').text('Pool week status changed.');

                    },
                    error: function (x, e) {
                        alert("The call to the server side failed. " + x.responseText);
                    }
                }
            );
   }
于 2012-10-04T17:19:29.477 回答