国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net Javascript獲取CheckBoxList的value

2020-01-18 01:02:14
字體:
來源:轉載
供稿:網友
以后我會陸續的寫出這段時間中學習到的東西,與大家一起分享。這篇文章也算是工作中的一個筆記吧,希望給遇到同樣問題的朋友,一點小小的幫助。
在 開發工作中,因為要做用到CheckBoxList在客戶端用js操作,無論js怎樣調試,就是無法獲取value的值,很是郁悶,后來Google了下,去了趟CodeProject,算是幸運的。我們在網頁上放置一下代碼:
復制代碼 代碼如下:

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

結束
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河津市| 富川| 通山县| 扶余县| 泗阳县| 长丰县| 花莲县| 曲麻莱县| 博湖县| 明溪县| 宁陵县| 石渠县| 鄂尔多斯市| 治县。| 林芝县| 南涧| 印江| 赫章县| 苍南县| 女性| 河池市| 嵊州市| 巴马| 新晃| 黑山县| 株洲市| 永德县| 遵义市| 永善县| 乐至县| 甘洛县| 五河县| 巴林右旗| 东阿县| 广德县| 深水埗区| 攀枝花市| 板桥市| 凉山| 都兰县| 陕西省|