JQuery设置与获取RadioButtonList和CheckBoxList的值
有这样一个问题,要获取ASP.NET控件RadioButtonList的值,首先想到的就是$("#<%=RadioButtonList1.ClientID %>").val();结果返回为空。于是在浏览器查看HTML文本:

发现RadioButtonList和CheckBoxList都被解析为Table,并且每个子项由一个radio(checkbox)和label构成,label保存文本信息。
于是想到了下面的方法:
$(document).ready(function () {
$("#btnSelRadioList").click(function () {
var sValue = $("#<%=RadioButtonList1.ClientID %>").find("input[type='radio']:checked").val();
var sText = $("#<%=RadioButtonList1.ClientID %>").find("input[type='radio']:checked").next().text()
alert(sValue + "|" + sText);
});
$("#btnSelCheckBoxList").click(function () {
var sValue = "";
var sText = "";
$("#<%=CheckBoxList1.ClientID %>").find("input[type='checkbox']:checked").each(function () {
sValue += $(this).val() + ";";
sText += $(this).next().text() + ";";
})
alert(sValue + "|" + sText);
});
});
CheckBoxList可能会多选,所有需要遍历选中的项。上面的几句js代码顺利地取到了值。

设置默认选中的值:
//设置RadioButtonList1第二项选中
$("#<%=RadioButtonList1.ClientID %>").find("input[type='radio']")[].checked = true; //设置CheckBoxList1第二、三项选中
$("#<%=CheckBoxList1.ClientID %>").find("input[type='checkbox']")[].checked = true;
$("#<%=CheckBoxList1.ClientID %>").find("input[type='checkbox']")[].checked = true;
完整的代码:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () { //设置RadioButtonList1第二项选中
$("#<%=RadioButtonList1.ClientID %>").find("input[type='radio']")[].checked = true; //设置CheckBoxList1第二、三项选中
$("#<%=CheckBoxList1.ClientID %>").find("input[type='checkbox']")[].checked = true;
$("#<%=CheckBoxList1.ClientID %>").find("input[type='checkbox']")[].checked = true; $("#btnSelRadioList").click(function () {
var sValue = $("#<%=RadioButtonList1.ClientID %>").find("input[type='radio']:checked").val();
var sText = $("#<%=RadioButtonList1.ClientID %>").find("input[type='radio']:checked").next().text() alert(sValue + "|" + sText);
}); $("#btnSelCheckBoxList").click(function () {
var sValue = "";
var sText = "";
$("#<%=CheckBoxList1.ClientID %>").find("input[type='checkbox']:checked").each(function () {
sValue += $(this).val() + ";";
sText += $(this).next().text() + ";";
}) alert(sValue + "|" + sText);
});
});
</script>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="">北京</asp:ListItem>
<asp:ListItem Value="">上海</asp:ListItem>
<asp:ListItem Value="">南京</asp:ListItem>
</asp:RadioButtonList>
<input id="btnSelRadioList" type="button" value="RadioButtonList选中项" />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="">北京</asp:ListItem>
<asp:ListItem Value="">上海</asp:ListItem>
<asp:ListItem Value="">南京</asp:ListItem>
</asp:CheckBoxList>
<input id="btnSelCheckBoxList" type="button" value="CheckBoxList选中项" />
JQuery设置与获取RadioButtonList和CheckBoxList的值的更多相关文章
- jQuery设置和获取HTML、文本和值
jQuery设置和获取HTML.文本和值 按 Ctrl+C 复制代码 <script type="text/javascript"> //<![CDATA[ $( ...
- jQuery设置和获取以及修改class name值操作
在Web程序开发中.很多时候会用需要修改Html标签的class名称.来达到修改标签样式的效果.那么在代码中一般是怎么操作的呢.本文将为你详细讲解一下class的使用.在jQuery中可以使用attr ...
- jquery尺寸和jQuery设置和获取内容方法
一.jquery尺寸 jQuery 提供多个处理尺寸的重要方法: width() 设置或返回元素的宽度(不包括内边距.边框或外边距),括号中可填数值宽度参数,无单位 height() 设置或 ...
- js设置、获取单值cookie和多值cookie
js设置.获取单值cookie和多值cookie,代码如下: var CookieUtil = (function () { var Cookie = function () { // 获取单值coo ...
- js 得到 radiobuttonlist和CheckBoxList 选中值
js 得到 radiobuttonlist和CheckBoxList 选中值 得到radiobuttonlist 选中值:var CheckBoxList=document.all.optButton ...
- jQuery的DOM操作之设置和获取HTML、文本和值 html()text()val()
1. html()方法: 此方法类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的html内容. <html> <head> <met ...
- jquery设置div,文本框 表单的值示例
我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容html() - 设置或返回所选元素的内容(包括 HTML标记)val() - 设置或返回表单字段的值 1 ...
- c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
/// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...
- 使用JavaScript设置、获取父子页面中的值
一:获取父页面中的值 有二种方法windows.open()和windows.showModalDialog() 1.windos.open(URL,name,reatures,replace) 再父 ...
随机推荐
- php连接ftp
PHP连接ftp,发现一个很好用的类库phpseclib.英文原文 Connecting to SFTP with PHP If you need to connect to SFTP using P ...
- Mindjet.MindManager“参数错误”解决办法,适用于9.0、10.0和14.0
MindManager出14.0版本了,但是在应用个别模板的时候会提示“参数错误”,然后自动关闭. 解决办法: 如果是win7系统,可以进入C:\Users\(用户名) \AppData\Loca ...
- Linux编程学习笔记 -- Process
进程是一个程序的运行. 在一个程序中执行另一个执程序的方法有两种: 1)system 在shell中执行程序 2)fork + exec 复制一个进程,在进程中用新的程序替换原有的程序 for ...
- Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming 在Spark2.x中,Spark Streaming获得了比较全面的升级,称为St ...
- CentOS安装,更新Python
1.查看当前Python版本 python -V 2.查看当前CentOS版本 cat /etc/redhat-release 3.安装所有的开发工具包 yum groupinstall " ...
- 使用OpenXml把Excel中的数据导出到DataSet中
public class OpenXmlHelper { /// <summary> /// 读取Excel数据到DataSet中,默认读取所有Sheet中的数据 /// </sum ...
- [转]Unity3D游戏开发之数据持久化PlayerPrefs的使用
转自 http://blog.csdn.net/qinyuanpei/article/details/24195977 首先我们来看两段Unity3D中实现数据读写的简单代码吧: //保存数据 Pla ...
- Mysql 备份恢复之 Mysqldump 工具
目前正在学习中,看到mysqldump工具导出的数据都是文本形式的,如果是blob或text大对象类型导出的是什么格式的?这个需要后续研究.下面只先总结下简单的. 一.备份1.备份Mysql一个数据库 ...
- 1509 -- Glass Beads POJ
题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...
- About javascript closure
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures?redirectlocale=en-US&redi ...