解決無法用Javascript取得CheckBoxList Value的問題
2010-04-22 14:44
525 查看
解決無法用Javascript取得CheckBoxList Value的問題
先放置一個測試用CheckBoxList<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="小明" Value="A"></asp:ListItem>
<asp:ListItem Text="小華" Value="B"></asp:ListItem>
<asp:ListItem Text="小張" Value="C"></asp:ListItem>
<asp:ListItem Text="小菜" Value="D"></asp:ListItem>
</asp:CheckBoxList>
執行後,觀看程式碼
<table id="CheckBoxList1" border="0">
<tr>
<td><input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" /><label for="CheckBoxList1_0">小明</label></td>
</tr><tr>
<td><input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" /><label for="CheckBoxList1_1">小華</label></td>
</tr><tr>
<td><input id="CheckBoxList1_2" type="checkbox" name="CheckBoxList1$2" /><label for="CheckBoxList1_2">小張</label></td>
</tr><tr>
<td><input id="CheckBoxList1_3" type="checkbox" name="CheckBoxList1$3" /><label for="CheckBoxList1_3">小菜</label></td>
</tr>
</table>
由上面的結果我們可以看到,input 裡並沒有Value的值可以讓JavaScript抓取,所以現在我們要幫他加一個attribute
protected void Page_Load(object sender, EventArgs e)
...{
foreach (ListItem li in CheckBoxList1.Items)
li.Attributes["MainValue"] = li.Value;
}
在觀看一次程式碼
<table id="CheckBoxList1" border="0">
<tr>
<td><span MainValue="A"><input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" /><label for="CheckBoxList1_0">小明</label></span></td>
</tr><tr>
<td><span MainValue="B"><input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" /><label for="CheckBoxList1_1">小華</label></span></td>
</tr><tr>
<td><span MainValue="C"><input id="CheckBoxList1_2" type="checkbox" name="CheckBoxList1$2" /><label for="CheckBoxList1_2">小張</label></span></td>
</tr><tr>
<td><span MainValue="D"><input id="CheckBoxList1_3" type="checkbox" name="CheckBoxList1$3" /><label for="CheckBoxList1_3">小菜</label></span></td>
</tr>
</table>
我們可以發現,input被Span包住了,而且裡面多了一個我們剛剛設定的MainValue Attribute,現在我們就可以引用Jquery來取值了
$("#CheckBoxList1 input:checkbox").each(function(){
$(this).parent('span').attr('MainValue');
});
相关文章推荐
- Javascript获取CheckBoxList的value
- asp.net Javascript获取CheckBoxList的value
- JavaScript 获取CheckBoxList的value(转载)
- Javascript如何获取CheckBoxList的value
- Javascript得到CheckBoxList的Value
- asp.net Javascript获取CheckBoxList的value
- javascript控制服务器控件-js操作CheckBoxList实现全选、反选
- jQuery获取checkboxlist的value值的方法
- How to get value from TextBox, RadioButtonList, DropDownList, CheckBox through JavaScript
- Javascript如何获取CheckBoxList控件列表项的值?
- js 取得CheckBoxList的选中项的值
- 转:解決在 Visual Studio 中因為權限不足而無法建置網站的問題
- javascript控制服务器控件-js操作CheckBoxList实现全选、反选
- Jquery 获取Asp.Net CheckBoxList的Value,Text
- 利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題
- 安裝VB時,出現MSADDNDR.DLL無法注冊問題的解決方法
- JavaScript取得select option value
- 解決JavaScript中replace只置換第一個符合的字串問題
- ASP.NET CheckBoxList 用javascript 实现全选
- asp.net绑定checkboxlist--------jquery绑定click事件,获取checkbox的value和显示值