只有在Jquery中检查时才处理Asp:CheckBoxList的onClick

时间:2012-10-01 23:32:43

标签: jquery asp.net onclick checkboxlist

我有一个动态生成的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");
}

我看到的所有样本都找到了所有选中的复选框并循环遍历它们,再次我只想要触发该功能的SPECIFIC复选框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>

1 个答案:

答案 0 :(得分:1)

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

   $(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);
                    }
                }
            );
   }