有这样一个问题,要获取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的值的更多相关文章

  1. jQuery设置和获取HTML、文本和值

    jQuery设置和获取HTML.文本和值 按 Ctrl+C 复制代码 <script type="text/javascript"> //<![CDATA[ $( ...

  2. jQuery设置和获取以及修改class name值操作

    在Web程序开发中.很多时候会用需要修改Html标签的class名称.来达到修改标签样式的效果.那么在代码中一般是怎么操作的呢.本文将为你详细讲解一下class的使用.在jQuery中可以使用attr ...

  3. jquery尺寸和jQuery设置和获取内容方法

    一.jquery尺寸 jQuery 提供多个处理尺寸的重要方法: width()    设置或返回元素的宽度(不包括内边距.边框或外边距),括号中可填数值宽度参数,无单位 height()   设置或 ...

  4. js设置、获取单值cookie和多值cookie

    js设置.获取单值cookie和多值cookie,代码如下: var CookieUtil = (function () { var Cookie = function () { // 获取单值coo ...

  5. js 得到 radiobuttonlist和CheckBoxList 选中值

    js 得到 radiobuttonlist和CheckBoxList 选中值 得到radiobuttonlist 选中值:var CheckBoxList=document.all.optButton ...

  6. jQuery的DOM操作之设置和获取HTML、文本和值 html()text()val()

    1. html()方法: 此方法类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的html内容. <html> <head> <met ...

  7. jquery设置div,文本框 表单的值示例

    我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容html() - 设置或返回所选元素的内容(包括 HTML标记)val() - 设置或返回表单字段的值 1 ...

  8. c# 根据配置文件路径,设置和获取config文件 appSettings 节点值

    /// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...

  9. 使用JavaScript设置、获取父子页面中的值

    一:获取父页面中的值 有二种方法windows.open()和windows.showModalDialog() 1.windos.open(URL,name,reatures,replace) 再父 ...

随机推荐

  1. CentOS-6.5安装配置JDK-7|Tomcat-8

    安装说明 系统环境:centos-6.5 安装方式:rpm安装 软件:jdk-7-linux-x64.rpm 下载地址:http://www.oracle.com/technetwork/java/j ...

  2. Python urllib2 模块学习笔记

    2015.3.6  urllib2的使用方法大致如下 # 定制Handler处理函数 opener = urllib2.build_opener(ProxyHandler, HTTPHandler) ...

  3. Django开发网站(二)

    第一课:视图显示 1   建立一个项目:django-admin startproject blog, 进入blog: cd blog 显示:blog(__init__.py settings.py ...

  4. 1037. Magic Coupon (25)

    #include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...

  5. 1062 Talent and Virtue (25)

    /* L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of ta ...

  6. fedora安装postgresql

    1. 安装:postgresql-server pgadmin程序 sudo yum install postgresql-server.x86_64 sudo yum install pgadmin ...

  7. .htaccess 设置

     RewriteEngine on RewriteCond %{HTTP_HOST} ^blog.chosenet.com$RewriteCond %{REQUEST_URI} !^/blog/Rew ...

  8. Linux中Kill进程的N种方法

    常规篇: 首先,用ps查看进程,方法如下: $ ps -ef …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx ...

  9. Code for the Homework2 改进

    1. 实现了到指定点各个关节的转角计算(多解性),并且所求解满足各个关节的最大角和最小角的限制条件. 2. 对方向向量进行了单位化,保证任意大小的向量都行 #include<iostream&g ...

  10. JS创建类和对象

    JavaScript 创建类/对象的几种方式 在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活 ...