CheckBox為CheckBoxList實(shí)現(xiàn)全選或全取消選擇(js代碼實(shí)現(xiàn))
更新時(shí)間:2013年01月23日 10:07:51 作者:
在管理商品后臺(tái)是,由于CheckBoxList的選擇太多,用戶需要一個(gè)全選或全取消的功能,這樣操作起來(lái)會(huì)提高效率同時(shí)可以減少誤點(diǎn)等,本文將教大家如何實(shí)現(xiàn),有需要的朋友可以參考下,望本文對(duì)你有所幫助
某一個(gè)時(shí)候,CheckBoxList的選擇太多,用戶需要一個(gè)全選或全取消的功能。下面使用Javascript來(lái)實(shí)現(xiàn)它。
準(zhǔn)備好一個(gè)對(duì)象:
MusicType
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for MusicType
/// </summary>
namespace Insus.NET
{
public class MusicType
{
private int _ID;
private string _TypeName;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string TypeName
{
get { return _TypeName; }
set { _TypeName = value; }
}
public MusicType()
{
//
// TODO: Add constructor logic here
//
}
public MusicType(int id, string typeName)
{
this._ID = id;
this._TypeName = typeName;
}
}
}
填充對(duì)象:
public List<MusicType> GetMusicType()
{
List<MusicType> mt = new List<MusicType>();
mt.Add(new MusicType(1, "甜密情歌"));
mt.Add(new MusicType(2, "網(wǎng)絡(luò)紅歌"));
mt.Add(new MusicType(3, "兒童歌曲"));
mt.Add(new MusicType(4, "民族精選"));
mt.Add(new MusicType(5, "校園歌曲"));
mt.Add(new MusicType(6, "搖滾音樂(lè)"));
mt.Add(new MusicType(7, "胎教音樂(lè)"));
mt.Add(new MusicType(8, "紅色名曲"));
mt.Add(new MusicType(9, "串燒金曲"));
mt.Add(new MusicType(10, "動(dòng)慢歌曲"));
return mt;
}
在站點(diǎn)建一個(gè)aspx網(wǎng)頁(yè),并拉兩個(gè)控件,一個(gè)是CheckBox和CheckBoxList:
全選<asp:CheckBox ID="CheckBoxAll" runat="server" onClick="javascript:Check_Uncheck_All(this);" /><br />
<asp:CheckBoxList ID="CheckBoxListMusicType" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="300"></asp:CheckBoxList>
接下來(lái),我們?yōu)镃heckBoxList綁定數(shù)據(jù):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
this.CheckBoxListMusicType.DataSource = GetMusicType();
this.CheckBoxListMusicType.DataTextField = "TypeName";
this.CheckBoxListMusicType.DataValueField = "ID";
this.CheckBoxListMusicType.DataBind ();
}
}
最后是寫(xiě)Javascript代碼:
<script type="text/javascript">
function Check_Uncheck_All(cb) {
var cbl = document.getElementById("<%=CheckBoxListMusicType.ClientID%>");
var input = cbl.getElementsByTagName("input");
if (cb.checked) {
for (var i = 0; i < input.length; i++) {
input[i].checked = true;
}
}
else {
for (var i = 0; i < input.length; i++) {
input[i].checked = false;
}
}
}
</script>
ok完成,看看效果:
準(zhǔn)備好一個(gè)對(duì)象:
MusicType
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for MusicType
/// </summary>
namespace Insus.NET
{
public class MusicType
{
private int _ID;
private string _TypeName;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string TypeName
{
get { return _TypeName; }
set { _TypeName = value; }
}
public MusicType()
{
//
// TODO: Add constructor logic here
//
}
public MusicType(int id, string typeName)
{
this._ID = id;
this._TypeName = typeName;
}
}
}
填充對(duì)象:
復(fù)制代碼 代碼如下:
public List<MusicType> GetMusicType()
{
List<MusicType> mt = new List<MusicType>();
mt.Add(new MusicType(1, "甜密情歌"));
mt.Add(new MusicType(2, "網(wǎng)絡(luò)紅歌"));
mt.Add(new MusicType(3, "兒童歌曲"));
mt.Add(new MusicType(4, "民族精選"));
mt.Add(new MusicType(5, "校園歌曲"));
mt.Add(new MusicType(6, "搖滾音樂(lè)"));
mt.Add(new MusicType(7, "胎教音樂(lè)"));
mt.Add(new MusicType(8, "紅色名曲"));
mt.Add(new MusicType(9, "串燒金曲"));
mt.Add(new MusicType(10, "動(dòng)慢歌曲"));
return mt;
}
在站點(diǎn)建一個(gè)aspx網(wǎng)頁(yè),并拉兩個(gè)控件,一個(gè)是CheckBox和CheckBoxList:
復(fù)制代碼 代碼如下:
全選<asp:CheckBox ID="CheckBoxAll" runat="server" onClick="javascript:Check_Uncheck_All(this);" /><br />
<asp:CheckBoxList ID="CheckBoxListMusicType" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="300"></asp:CheckBoxList>
接下來(lái),我們?yōu)镃heckBoxList綁定數(shù)據(jù):
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
this.CheckBoxListMusicType.DataSource = GetMusicType();
this.CheckBoxListMusicType.DataTextField = "TypeName";
this.CheckBoxListMusicType.DataValueField = "ID";
this.CheckBoxListMusicType.DataBind ();
}
}
最后是寫(xiě)Javascript代碼:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function Check_Uncheck_All(cb) {
var cbl = document.getElementById("<%=CheckBoxListMusicType.ClientID%>");
var input = cbl.getElementsByTagName("input");
if (cb.checked) {
for (var i = 0; i < input.length; i++) {
input[i].checked = true;
}
}
else {
for (var i = 0; i < input.length; i++) {
input[i].checked = false;
}
}
}
</script>
ok完成,看看效果:
您可能感興趣的文章:
- 比較全的JS checkbox全選、取消全選、刪除功能代碼
- Javascript實(shí)現(xiàn)CheckBox的全選與取消全選的代碼
- js multiple全選與取消全選實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)復(fù)選框的全選和取消全選效果
- javascript 全選/反選,取消選擇效果
- 基于JavaScript實(shí)現(xiàn)復(fù)選框的全選和取消全選
- JS小功能(checkbox實(shí)現(xiàn)全選和全取消)實(shí)例代碼
- JS控件ASP.NET的treeview控件全選或者取消(示例代碼)
- javascript 全選與全取消功能的實(shí)現(xiàn)代碼
- JS實(shí)現(xiàn)表單全選以及取消全選實(shí)例
相關(guān)文章
ASP.net?Core微信平臺(tái)開(kāi)發(fā)配置Token
這篇文章主要為大家介紹了ASP.net?Core微信平臺(tái)開(kāi)發(fā)配置Token有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
.NET 6開(kāi)發(fā)TodoList應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)塑形
在查詢的場(chǎng)景中,還有一類(lèi)需求不是很常見(jiàn),就是在前端請(qǐng)求中指定返回的字段。所以這篇文章主要介紹了.NET 6如何實(shí)現(xiàn)數(shù)據(jù)塑形,需要的可以參考一下2022-01-01
高效的使用 Response.Redirect解決一些不必要的問(wèn)題
這篇文章主要介紹了如何高效的使用 Response.Redirect解決一些不必要的問(wèn)題,需要的朋友可以參考下2014-05-05
asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的圖片或水印的方法
本文詳細(xì)講解如何使用Aspose.Words for .NET的組件來(lái)生成word文檔與水印的方法,請(qǐng)看本文內(nèi)容。2010-04-04
System.Timers.Timer定時(shí)執(zhí)行程序示例代碼
如果是某個(gè)邏輯功能的定時(shí),可以將code放到邏輯功能的類(lèi)的靜態(tài)構(gòu)造函數(shù)中,在該邏輯類(lèi)第一次執(zhí)行時(shí),靜態(tài)構(gòu)造函數(shù)會(huì)被調(diào)用,則定時(shí)自然啟動(dòng)2013-06-06

