Javascript Get or Set Checked Radio Value
2015-01-08 09:40
204 查看
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
- get or set radio value
- HOWTO get multiple value from the same name checkbox elements or radiobution elements
- JavaScript Get radio value
- MapX SetKeyField及GetKeyValue使用举例。
- C# 参考之上下文关键字:get、set、value (ZT)
- C# 参考之上下文关键字get、set、value、partial、where和yield
- Memcached expire 设置错误引起的set(key,exp,value)为true而get(key)为null的问题
- Set the value for lable use javascript
- Null value is eliminated by an aggregate or other SET operation and returned error code 0xC0202009
- CWnd::GetCheckedRadioButton (判断当前一组单选按钮中哪一个被选中了)
- 【转载】C# 参考之上下文关键字get、set、value、partial、where和yield
- javascript getComputedStyle,getPropertyValue,CurrentStyle说明
- 自定义Field Type的Control时,Field值得Get和Set是通过重载Value属性实现的
- javascript 获取radio的value的函数 [已测]
- Javascript verify integer or float value from textbox
- JavaScript操作DropDownList(Set value to dropdownlist with JavaScript)
- C# : row-clickable GridView and get and set gridview rows using JavaScript
- How to get multiple select value both in javascript and php
- <c:set var="root" value="${pageContext.request.contextPath}"></c:set>According to TLD or attribute d