阅读102 返回首页    go 微软 go windows


JQuery .Net CheckBoxList控件

很久没弄jQuery了,今天做项目时,遇到个问题:js取不到选中的CheckBoxList的ListItem的值。

 CheckBoxList前台解析为:

<table border="0"> <tr> <td> <input type="checkbox" name="listTest$0" /> <label for="listTest_0">item1</label> </td> </tr> <tr> <td><input type="checkbox" name="listTest$1" /> <label for="listTest_1">item2</label> </td> </tr> </table>

搜了下,网上有个可行的办法:给ListItem加alt属性(其他属性也行),通过获取属性值来获取选项值;

 

解决方案

在绑定checkboxlist时,为ListItem每个对象添加一个alt属性,值保存对应的value值,代码如下:

 

 if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { //分别为text值、value值 listTest.Items.Add(new ListItem(dr["Title"].ToString(), dr["ID"].ToString())); } //为ListItem对象添加alt属性,值保存value值 foreach (ListItem li in listTest.Items) { li.Attributes.Add("alt", li.Value); } } <table border="0"> <tr> <td> <span alt="400"><input type="checkbox" name="listTest$0" /> <label for="listTest_0">基于jQuery的一个震动效果</label></span> </td> </tr> <tr> <td><span alt="398"><input type="checkbox" name="listTest$1" /> <label for="listTest_1">使用css的overflow属性改变缩略图大小</label></span> </td> </tr> </table> 下面就是js取选中的listitem的值: $(document).ready(function() { $("#btnShow").click(function() { var valuelist = ""; //保存checkbox选中值 //遍历name以listTest开头的checkbox $("input[name^='listTest']").each(function() { if (this.checked) { //$(this):当前checkbox对象; //$(this).parent("span"):checkbox父级span对象 valuelist += $(this).parent("span").attr("alt") + ","; } }); if (valuelist.length > 0) { //得到选中的checkbox值序列,结果为400,398 valuelist = valuelist.substring(0, valuelist.length - 1); } }); });

关键点:valuelist += $(this).parent("span").attr("alt") + ",";

最后更新:2017-04-02 06:51:42

  上一篇:go 认识JVM--第二篇-java对象内存模型
  下一篇:go SQL Server 2000安装时不出现安装界面,进程中存在解决