jQuery获取checkboxlist值
问题:
当页面回传一次后,生成的<span>值就没有了,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 id="Table1" border="0">
<tr>
<td>
<span alt="jQuery获取checkboxlist值,该如何解决"><input id="listTest_0" type="checkbox" name="listTest$0" />
<label for="listTest_0">基于jQuery的一个震动效果</label></span>
</td>
</tr>
<tr>
<td><span alt="jQuery获取checkboxlist值,该如何解决"><input id="listTest_1" type="checkbox" name="listTest$1" />
<label for="listTest_1">使用css的overflow属性改变缩略图大小</label></span>
</td>
</tr>
</table>
$(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);
}
});
});
foreach (ListItem li in listTest.Items)
{
li.Attributes.Add("alt", li.Value);
}
int i = 0;
foreach (ListItem li in listTest.Items)
{
Response.Write(string.Format("<input type='hidden' id='{0}' class='alt' name='{0}' value='{1}' />", "alt_"+i, li.Value));
i++;
}