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的更多相关文章

  1. 【JavaScript&jQuery】单选框radio,复选框checkbox,下拉选择框select

    HTML: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...

  2. Jquery radio checked

    Jquery radio checked     radio 1. $("input[name='radio_name'][checked]").val(); //选择被选中Rad ...

  3. JavaScript jQuery 入门回顾

    ​$符号 $是著名的jQuery符号.实际上,jQuery把所有功能全部封装在一个全局变量jQuery中,而$也是一个合法的变量名,它是变量jQuery的别名: window.jQuery; // j ...

  4. 自定义radio图标

    问题: 默认的radio控件不是很好看,我们能否自定义一个radio图标? 解决: 1.radio有input和lable两个标签. 2.<input>是前面的图标,选中后图标变化. 3. ...

  5. radio(单选框)反复选中与取消选中

    做个记录,以便需要拿取 <script type="text/javascript"> $(function(){ 第一种 $('input:radio').click ...

  6. 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 ...

  7. JQuery控制radio选中和不选中方法总结

    一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...

  8. 表单:checkbox、radio样式(用图片换掉默认样式)

    checkbox.radio样式(用图片换掉默认样式) <!doctype html> <html> <head> <meta charset="u ...

  9. js之radio应用实例

    radio和checkbox还有select,可谓是前后端常用三剑客啊!特别是checkbox和select,关于这两个今天不讲,因为在下面这几篇文章,我已经比较详细的讲解了. SpringMVC之a ...

随机推荐

  1. dict扩展munch,支持yaml文件

    安装:pip install munch 用法参考:https://github.com/Infinidat/munch Munch is a dictionary that supports att ...

  2. 【UOJ Easy Round #1】

    数论/Trie/并查集 猜数 这题我是这样分析的…… $a*b=g*l=n=k^2 \ and \ (g|a,g|b) \Rightarrow (g*a')*(g*b' )=g*l=k^2 \\ \R ...

  3. Ios开发之Category

    Category是在不改变已存在类的情况下,对其添加方法来达到对类进行功能扩展的目的. 对类功能进行拓展的时候,我们会有多种方式,比如说可以通过继承也可以进行功能扩展,但是在Category和继承上我 ...

  4. Python 通过打码平台实现验证码

    在爬虫时,经常遇到登录需要验证码的情况,简单的验证码可以自己解决,复制的验证码需要借助机器学习,有一定的难度.还有一个简单的方案就是采用付费的打码平台. 比如R若快(http://www.ruokua ...

  5. Spearman(斯皮尔曼) 等级相关

    Spearman相关系数又称秩相关系数,是利用两变量的秩次大小作线性相关分析,对原始变量的分布不作要求,属于非参数统计方法,适用范围要广些.对于服从Pearson相关系数的数据亦可计算Spearman ...

  6. linux命令学习——cat

    1.前言 今天需要处理一个oui.txt文件,需要从中抽丝man和orginaziton信息,导出到另外一个文件中.可以cat和grep命令进行操作.之前对cat命令了解一下,知道cat可以查看文件内 ...

  7. 什么是BFC(Block Formatting Context)

    原文:https://segmentfault.com/a/1190000012221820 https://www.w3.org/TR/CSS2/visuren.html#block-formatt ...

  8. python命令行参数传递JSON串

    有点小问题,一是传递的双引号被自动删除了,但是如果用单引号,JSON解析又不认. 所以,最后的方案是,传递单引号,但程序处理时做一个替换,替换成单引号.

  9. MongoDB的Invalid credentials for database

    前面都好好的,结果服务器数据库加了一个验证,查了一下,也不算复杂,只要把连接串一改就行了. 结果,不断报错——Invalid credentials for database 找了半天原因,原来是我用 ...

  10. 在SpringTest中将Mockito的mock对象通过spring注入使用

    转载:https://blog.csdn.net/m0_38043362/article/details/80111957 1. 原理介绍 通过BeanFactoryPostProcessor向Bea ...