• JavaScript 获取CheckBoxList的value(转载)


    <asp:CheckBoxList runat="server" ID="chkDemo" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <
    asp:ListItem Text="测试A" Value="A"></asp:ListItem>
    <
    asp:ListItem Text="测试B" Value="B"></asp:ListItem>
    <
    asp:ListItem Text="测试C" Value="C"></asp:ListItem>
    <
    asp:ListItem Text="测试D" Value="D"></asp:ListItem>
    <
    asp:ListItem Text="测试E" Value="E"></asp:ListItem>
    </
    asp:CheckBoxList>
    当浏览器呈现这段代码后,我们再看看是什么样的Html脚本:
    <table id="chkDemo" border="0">
    <
    tr>
    <
    td><input id="chkDemo_0" type="checkbox" name="chkDemo$0" /><label for="chkDemo_0">测试A</label></td>
            <td><input id="chkDemo_1" type="checkbox" name="chkDemo$1" /><label for="chkDemo_1">测试B</label></td>
            <td><input id="chkDemo_2" type="checkbox" name="chkDemo$2" /><label for="chkDemo_2">测试C</label></td>
            <td><input id="chkDemo_3" type="checkbox" name="chkDemo$3" /><label for="chkDemo_3">测试D</label></td>
            <td><input id="chkDemo_4" type="checkbox" name="chkDemo$4" /><label for="chkDemo_4">测试E</label></td>
    </
    tr>
    </
    table>
    这段Html脚本会因为RepeatLayout的设置有所差异,但是都有一个共同点,就是 生成的CheckBox没有value属性,
    所以在客户端用js是没办法获取值的
    为了解决这个问题,我们需要扩展一下CheckBoxList:这是我在CodeProject上找到的源码,时间久了,链接就不贴了吧。
    [ToolboxData("<{0}:CheckBoxListEx runat=\"server\"></{0}:CheckBoxListEx>")]
    public class CheckBoxListEx : CheckBoxList,IRepeatInfoUser
    {
    void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
    {
    string clientID = UniqueID + this.ClientIDSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo); //var

    writer.WriteBeginTag("input");
    writer.WriteAttribute("type", "checkbox");
    writer.WriteAttribute("name", UniqueID + this.IdSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo));
    writer.WriteAttribute("id", clientID);
    writer.WriteAttribute("value", Items[repeatIndex].Value);
    if (Items[repeatIndex].Selected)
    writer.WriteAttribute("checked", "checked");

    System.Web.UI.AttributeCollection attrs = Items[repeatIndex].Attributes;
    foreach (string key in attrs.Keys)
    {
    writer.WriteAttribute(key, attrs[key]);
    }
    writer.Write("/>");
    writer.Write("<label for='" + clientID + "'>");
    writer.Write(Items[repeatIndex].Text);
    writer.Write("</label>");

    }
    }
    上边的这段代码是我经过修改的,与原著中有些差别:clientID的生成以及Checked属性的添加等,我想这段代码不需要再详细的讲解了吧。
    把它编译成单独的类,在Toolbox上会自动出现,像使用那个正常的CheckBoxList一样,拖动到页面就可以了。
    在客户端,我们js取值大致如下:
    <script>
    function getDemoValue()
    {
    var
    els = document.getElementById("chkDemo");
    var vals= '';
    if (els != null) {
    var chks = els.getElementsByTagName("input");
    for (var k = 0, len = chks.length; k < len; k++) {
    var chk = chks[k];
    if (chk != null && chk.type == 'checkbox' && chk.checked) {
    vals+= ',' + chk.value;
    }
    }
    }
           if(vals.length>1)
              vals = vals.substring(1);
           return vals;
    }
    </script>
    转自:http://www.cnblogs.com/inforasc/archive/2009/12/07/1618277.html
  • 相关阅读:
    springboot获取application.yml中的配置信息
    springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)
    最近工作中的一些教训
    SAP BADI的“多次使用”(multiple use)
    SAP CRM 忠诚度相关表的关系图
    一个噩梦
    i.s.h.med Enhancement for cancelling an appointment
    一个冷门语言开发者的2016年总结和2017年目标
    装机的几个误区
    关于BAPI_PATIENT_CREATE(病患主数据创建)
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1676525.html
Copyright © 2020-2023  润新知