ASP.NET CheckBoxList Operations with jQuery

简介:

本文描述了如何通过jQuery来对ASP.NET CheckBoxList控件进行一些基本操作,如通过value/text/index check/uncheck CheckBoxList,最小/最大选择限制等。

  例如在ASP.NET页面中有如下CheckBoxList控件定义:

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<input type="button" value="OK" id="demo" />

  Server端代码:

复制代码
Dictionary<int,string>  dictItems = new Dictionary<int,string>();
 dictItems.Add(1, "Item-1");
 dictItems.Add(2, "Item-2");
 dictItems.Add(3, "Item-3");
 dictItems.Add(4, "Item-4");
 dictItems.Add(5, "Item-5");
 
 CheckBoxList1.DataSource = dictItems;
 CheckBoxList1.DataTextField = "Value";
 CheckBoxList1.DataValueField = "Key";
 CheckBoxList1.DataBind(); 
复制代码

  运行页面,在浏览器中你会看到上述代码会生成如下HTML片段:

复制代码
<table id="MainContent_CheckBoxList1">
    <tr>
        <td><input id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="1" /><label for="MainContent_CheckBoxList1_0">Item-1</label></td>
    </tr><tr>
        <td><input id="MainContent_CheckBoxList1_1" type="checkbox" name="ctl00$MainContent$CheckBoxList1$1" value="2" /><label for="MainContent_CheckBoxList1_1">Item-2</label></td>
    </tr><tr>
        <td><input id="MainContent_CheckBoxList1_2" type="checkbox" name="ctl00$MainContent$CheckBoxList1$2" value="3" /><label for="MainContent_CheckBoxList1_2">Item-3</label></td>
    </tr><tr>
        <td><input id="MainContent_CheckBoxList1_3" type="checkbox" name="ctl00$MainContent$CheckBoxList1$3" value="4" /><label for="MainContent_CheckBoxList1_3">Item-4</label></td>
    </tr><tr>
        <td><input id="MainContent_CheckBoxList1_4" type="checkbox" name="ctl00$MainContent$CheckBoxList1$4" value="5" /><label for="MainContent_CheckBoxList1_4">Item-5</label></td>
    </tr>
</table>
<input type="button" value="OK" id="demo" /> 
复制代码

   下面来看看如何通过jQuery对CheckBoxList控件进行操作。

1. 获取选中项的Value值

复制代码
//Get value of selected items
$("#demo").click(function () {      
    var selectedValues = [];
    $("[id*=CheckBoxList1] input:checked").each(function () {           
        selectedValues.push($(this).val());
    });
    if (selectedValues.length>0) {
        alert("Selected Value(s): " + selectedValues.toString());
    } else {
        alert("No item has been selected.");
    }
});
复制代码

2. 获取选中项的索引

复制代码
//Get index of selected items
   $("#demo").click(function () {
       var $ctrls = $("[id*=CheckBoxList1] input:checkbox");
       $("[id*=CheckBoxList1] input:checked").each(function () {
          alert($ctrls.index($(this)));
       });       
   });
复制代码

  注意索引是从0开始的,如果选中项是Item-1,Item-3,Item-4,则alert对话框中对应显示的内容是0,2,3.

3. 获取选中项的Text值

复制代码
//Get text of selected items
 $("#demo").click(function () {       
     $("[id*=CheckBoxList1] input:checked").each(function () {
         alert($(this).next().html());
     });
 });
复制代码

  查看对应的HTML代码,你会发现Text的值被存放在label控件中,该控件正好属于checkbox控件的下一个元素,因此我们可以通过$(this).next().html()方法来获取到它。

4. Check/Uncheck CheckBoxList的所有元素

$("[id*=CheckBoxList1] input:checkbox").prop('checked',true); //To check all
$("[id*=CheckBoxList1] input:checkbox").prop('checked',false);// To uncheck all

  jQuery 1.6以上版本使用prop()方法,1.6以下版本使用attr()方法。

5. 通过索引选中Checkbox

//Check Items by index
   var selIndex = [0, 2, 3];
   for (var i = 0; i < selIndex.length; i++) {
       $("[id*=CheckBoxList1] input:checkbox").eq(selIndex[i]).prop('checked', true);
   }

  同样,你可以在prop()方法中将第二个参数改为false来取消对Checkbox的选择。

6. 通过Value属性选中Checkbox

复制代码
//Check Items by value
   var selValue = [1, 2, 4];
   var $ctrls = $("[id*=CheckBoxList1]");
   for (var i = 0; i < selValue.length; i++) {
       $ctrls.find('input:checkbox[value=' + selValue[i] + ']').prop('checked', true);
   }
复制代码

  上面的代码中,如果Value值在selValue数组中存在则将对应的Checkbox选中。

7. 通过Text属性选中Checkbox

复制代码
//Check Items by Text
    var selText = ['Item-1','Item-3'];
    var $ctrls = $("[id*=CheckBoxList1]");
    for (var i = 0; i < selText.length; i++) {
        $ctrls.find('label:contains("' + selText[i] + '")').prev().prop('checked', true);
    }
复制代码

  上面的代码会查找CheckBoxList控件所生成的HTML代码中对应的label元素,如果该label元素的Text值在selText数组中存在则与之对应的Checkbox会被选中。本例中Item-1Item-3所对应的Checkbox会被选中。

8. 最大选中项限制

复制代码
$("[id*=CheckBoxList1] input:checkbox").change(function () {
          var maxSelection = 3;
          if ($("[id*=CheckBoxList1] input:checkbox:checked").length > maxSelection) {
              $(this).prop("checked", false);
              alert("Please select a maximum of " + maxSelection + " items.");
          }
      })
复制代码

  上面的代码允许CheckBoxList中最多只能有3项同时被选中。同样,你可以对代码进行适当修改以实现最小选中项限制。

 

  希望上面给出的代码能对日常编程工作提供一些帮助!


本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/p/3816683.html,如需转载请自行联系原作者

相关文章
|
2月前
|
开发框架 JSON JavaScript
ASP.NET Core3.1实战教程---基于Jquery单文件上传
ASP.NET Core3.1实战教程---基于Jquery单文件上传
51 0
|
开发框架 前端开发 JavaScript
ASP.Net Core中使用jquery-ajax-unobtrusive替换Ajax.BeginForm
ASP.Net Core中使用jquery-ajax-unobtrusive替换Ajax.BeginForm
170 0
|
开发框架 JavaScript 前端开发
【浅谈ASP.NET】——WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
【浅谈ASP.NET】——WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
181 0
【浅谈ASP.NET】——WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
|
前端开发 JavaScript API
NET MVC第七章、jQuery插件验证
NET MVC第七章、jQuery插件验证
158 0
NET MVC第七章、jQuery插件验证
|
开发框架 移动开发 前端开发
ASP.NET MVC中使用jQuery Ajax通过FormData对象异步提交图片文件到服务端保存并返回保存的图片路径
ASP.NET MVC中使用jQuery Ajax通过FormData对象异步提交图片文件到服务端保存并返回保存的图片路径
264 0
|
JSON 前端开发 .NET
如何构建ASP.NET MVC4&JQuery&AJax&JSon示例
背景:   博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax。 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Index动作方法添加视图(Index.cshtml),视图中HTML如下: 输入你的姓名: 输入你的年龄: 提交 清空   视图中包含两个文本框,分别用来输入名字和年龄,包含连个按钮,分别用来提交信息和清空文本框的内容,同时包含一个段落,用来显示Ajax返回的数据信息。
1065 0
|
JavaScript 前端开发 .NET
asp.net使用母版页以及Jquery和prototype要注意的问题
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1010885678/article/details/37504525 ...
982 0
|
2月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
100 0