如何使用jquery获取选定的Asp.net checkBoxList项目的索引及其文本值 [英] How to get the index of selected Asp.net checkBoxList items and the text value of it using jquery

查看:119
本文介绍了如何使用jquery获取选定的Asp.net checkBoxList项目的索引及其文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery获取所选ASP.NET CheckBoxList项目的索引及其文本值

How to get the index of selected ASP.NET CheckBoxList items and the text value of it using jQuery

我使用了列出的代码,但对我不起作用.

I used the code listed but it did not work with me.

前两行将返回带有"undefined"字符串的消息

First two lines will return message with "undefined" string

var index = $('#<%=chkListGroups.ClientID %>').attr("selectedIndex");
alert(index);

后两行将返回空消息.

var text = $('#<%=chkListGroups.ClientID %>').val();
alert(text);

推荐答案

var indices = '';
$('#<%=chkListGroups.ClientID %> input:checkbox').each(function (index) { 
   if ($(this).filter('input:checkbox:checked').length == 1) { 
       indices += (index + ' '); 
   }
});
alert(indices);

应打印索引.

如果用文本值"表示从ASP.NET中ListItem元素的Value属性返回的内容,则它不会直接自动包含在网页中.解决此问题的一种方法是在页面的.NET代码内创建Value属性的javascript数组,然后使用与上面类似的方法在数组中查找值.如果该数组称为"valueArray",则可以将上面"if"正文中的代码替换为

If by "text value" you mean what would be returned from the Value property of the ListItem element within ASP.NET, it is not directly included in the web page automatically. One way to address this issue would be to create a javascript array of the Value properties within the .NET code for the page, then use something similar to the above to look up the value in the array. If the array is called "valueArray", then you would could replace the code in the body of the "if" above with

indices += (valueArray[index] + ' ');

我希望这会有所帮助-我当然欢迎任何问题.

I hope this helps -- I certainly welcome any questions.

编辑(由提交者发表评论):

EDIT (following comment by submitter):

如果您希望每个已检查项目都带有可见标签,这应该可以:

If you want the visible label of each checked item, this should work:

var text = $('#<%=chkListGroups.ClientID %> input:checkbox:checked').siblings('label').text();
alert(text);

这篇关于如何使用jquery获取选定的Asp.net checkBoxList项目的索引及其文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆