1

请让我知道我可以在这里纠正什么,我被锁定在看似简单的事情上。

使用 jQuery 1.6.2:

我正在尝试返回 CheckBoxList 中选中项目的 VALUES 数组。

使用来自网络的修改示例,我有以下内容,它返回一个“on,on,on,on”数组(如果选中了所有 4 个复选框)。

Shots.push($(this).prop('checked')) 返回“true, true...” Shots.push($(this).attr('Value')) 返回“on, on...”

            var Shots = [];

            $('#cblShots').find('input[type=checkbox]:checked').each(function () {
                Shots.push($(this).val());
            });

                if (Shots != "") {
                    $(Pnote).val(Shots.join(', '));
                } else {
                    updateTips("Please select at least one item");
                    return false;
                }


<asp:Panel ID="pnlAppointmentShotSelection" runat="server" style="display:none;">
    <label>Please select which shots you would like administered</label>
    <br /><br />
    <asp:CheckBoxList ID="cblShots" runat="server" CssClass="ShotList">
      <asp:ListItem Value="Flu">Flu</asp:ListItem>
      <asp:ListItem Value="B-12">B-12</asp:ListItem>
      <asp:ListItem Value="Pneumonia">Pneumonia</asp:ListItem>
      <asp:ListItem Value="TDAP">Tetanus-Diptheria-Pertusis (TDAP)</asp:ListItem>
    </asp:CheckBoxList>
   </asp:Panel>
4

1 回答 1

0

根据这篇文章,可以向 ListItem 添加数据属性,因此请尝试向每个元素添加数据名称属性,然后在 javascript 中尝试:

var Shots = [];
$('#cblShots').find('input[type=checkbox]:checked').each(function () {
      Shots.push($(this).attr("data-name"));
});
于 2013-09-29T16:44:31.880 回答