Javascript Get or Set Checked Radio Value
Description
This pair of Javascript function can get or set the checked value of a group of radio buttons. These functions are specially designed for dynamic pages, and work without error with zero, one, or more radio buttons. Also, because the radio length is saved before looping, this function is much faster. Finally, the functions are granted to the public domain.
Source Code
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i < radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
radioObj.checked = (radioObj.value == newValue.toString());
return;
}
for(var i = 0; i < radioLength; i++) {
radioObj[i].checked = false;
if(radioObj[i].value == newValue.toString()) {
radioObj[i].checked = true;
}
}
}
Example
Use the view source command in your browser to see how the code below works. Note the use of the label HTML tag with a style that gives it a border and background color. You should always use the label tag with radio buttons. This signals to the user that the whole boxed area may be clicked to set the radio button, rather than just the tiny circle.
If you are using a scripting language like PHP, ColdFusion, or ASP, then write a function that prints your radio buttons from an array. It can automatically write the for and id properties by concatenating the radio name and value.
Zero One Two Three Four
Javascript Get or Set Checked Radio Value的更多相关文章
- 【JavaScript&jQuery】单选框radio,复选框checkbox,下拉选择框select
HTML: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...
- Jquery radio checked
Jquery radio checked radio 1. $("input[name='radio_name'][checked]").val(); //选择被选中Rad ...
- JavaScript jQuery 入门回顾
$符号 $是著名的jQuery符号.实际上,jQuery把所有功能全部封装在一个全局变量jQuery中,而$也是一个合法的变量名,它是变量jQuery的别名: window.jQuery; // j ...
- 自定义radio图标
问题: 默认的radio控件不是很好看,我们能否自定义一个radio图标? 解决: 1.radio有input和lable两个标签. 2.<input>是前面的图标,选中后图标变化. 3. ...
- radio(单选框)反复选中与取消选中
做个记录,以便需要拿取 <script type="text/javascript"> $(function(){ 第一种 $('input:radio').click ...
- How to get the value of a form element : check box and radio button
Getting a radio element and it’s checked value Radio buttons in a form can be grouped together using ...
- JQuery控制radio选中和不选中方法总结
一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...
- 表单:checkbox、radio样式(用图片换掉默认样式)
checkbox.radio样式(用图片换掉默认样式) <!doctype html> <html> <head> <meta charset="u ...
- js之radio应用实例
radio和checkbox还有select,可谓是前后端常用三剑客啊!特别是checkbox和select,关于这两个今天不讲,因为在下面这几篇文章,我已经比较详细的讲解了. SpringMVC之a ...
随机推荐
- pssh,pdsh,mussh,cssh,dsh运维工具介绍
pssh 1 安装:#wget http://peak.telecommunity.com/dist/ez_setup.pypython ez_setup.py#wget http://paralle ...
- oracle 索引的(创建、简介、技巧、怎样查看)
一.索引简介1.索引相当于目录2.索引是通过一组排序后的索引键来取代默认的全表扫描检索方式,从而提高检索效率.3.索引的创建要适度,多了会影响增删改的效率,少了会影响查询的效率,索引最好创建在取值分散 ...
- WinPcap权威指南(三):ARP协议
ARP协议在局域网内使用的非常广泛,它的数据包类型分为请求包和答复包.Windows系统内部有一个缓冲区,保存了最近的ARP信息,可以在cmd下使用命令arp -a来显示目前的缓存,或者使用命令arp ...
- php7安装mongoDB扩展
本文我们使用pecl命令来安装 首先来到php7的安装目录 $ /usr/local/php7/bin/pecl install mongodb 回车,执行成功后,会输出以下结果: …… Build ...
- Android -- 屏幕亮度
获取屏幕亮度 int getScreenBrightness(Activity activity) { int value = 0; ContentResolver cr = activity.get ...
- 利用Docker搭建java项目开发环境
一.需求 一台 Ubuntu 16.0.4 LTS ,安装了Docker服务,Rancher服务,也制作了Tomcat相关的image,接下来我们就来说一下如何快速的构建一个开发环境和测试环境 二.步 ...
- ps叠加模式笔记
1.叠加模式:Overlay混色模式会让图层白色的部分去加亮底色,图层深色的部分去加暗底色 2.常见按钮:1)主体渐变:2)投影:3)内阴影:颜色减淡,按钮颜色,距离1,大小3:4)外发光,正片叠底,
- VS2013创建Node.js C++ Addons的过程
首先看我的Node.js版本. node –v v6.11.4 然后参照这篇文章来做: https://nodejs.org/api/addons.html#addons_hello_world 安装 ...
- 保存登录plsql developer 的用户名和密码
1 保存用户名 tools -> Preferences -> User Interface - Options 勾选 Autosave username . 保存 2 保存密码 tool ...
- centos7默认安装没有连接到网络
1.显示所有连接 # nmcli con show 2.连接到网络 # nmcli con up enp0s3 这个ens33是通过显示所有连接查到的