【问题标题】:JQuery not returning Values from checkboxlist, returns stateJQuery 不从复选框列表返回值,返回状态
【发布时间】:2013-09-29 16:29:53
【问题描述】:

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

使用 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>

【问题讨论】:

  • 能否添加浏览器上显示的最终html代码?
  • 生成输出的第一行示例:

标签: javascript jquery asp.net


【解决方案1】:

根据这个post 可以将数据属性添加到 ListItem,所以尝试为每个元素添加数据名称属性,然后在 javascript 中尝试:

var Shots = [];
$('#cblShots').find('input[type=checkbox]:checked').each(function () {
      Shots.push($(this).attr("data-name"));
});

【讨论】:

  • @user1457613 可以添加浏览器上显示的最终html代码吗?
  • 在 IE 中生成的表 'cblShots' 示例:跨度>
  • 除了 name 不是 ASP.NET listitem 的有效属性。
  • @user1457613 尝试使用数据属性,我更新了我的答案以使用数据属性
猜你喜欢
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 2016-01-26
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多