`

Javascript Get or Set Checked Radio Value

阅读更多
<script type="text/javascript">
<!--
// 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;
}
}
}

//-->
</script>

<form name="radioExampleForm" method="get" action="" onsubmit="return false;">
<p><label for="number0"><input type="radio" value="0" name="number" id="number0"> Zero</label>
&nbsp;<label for="number1"><input type="radio" value="1" name="number" id="number1"> One</label>
&nbsp;<label for="number2"><input type="radio" value="2" name="number" id="number2"> Two</label>
&nbsp;<label for="number3"><input type="radio" value="3" name="number" id="number3"> Three</label>
&nbsp;<label for="number4"><input type="radio" value="4" name="number" id="number4"> Four</label>
<p><input type="button" onclick="alert('Checked value is: '+getCheckedValue(document.forms['radioExampleForm'].elements['number']));" value="Show Checked Value">
&nbsp;<input type="button" onclick="setCheckedValue(document.forms['radioExampleForm'].elements['number'], '2');" value="Set Checked to Two">
&nbsp;<input type="button" onclick="setCheckedValue(document.forms['radioExampleForm'].elements['number'], '4');" value="Set Checked to Four">
&nbsp;<input type="button" onclick="setCheckedValue(document.forms['radioExampleForm'].elements['number'], '');" value="Uncheck All">
</form>
分享到:
评论

相关推荐

    javascript对象与数组参考大全

    根据提供的文件信息,我们可以从标题、描述以及部分内容中提取出关于JavaScript对象与数组的重要知识点。 ### JavaScript对象与数组参考大全 #### 一、JavaScript对象详解 **1. Anchor对象** - **简介**: `...

    fso浏览54646465465464564

    '========================== ...&lt;tr&gt;&lt;td align=right&gt;类型:&lt;/td&gt;&lt;td&gt;&lt;input type="radio" name="ntype" checked value="0"&gt;文件夹 &lt;input type="radio" name="ntype" value="1"&gt;文件 &lt;tr&gt;&lt;td align=right&gt;名称: ...

    jQuery对html元素取值与赋值的方法

    - 选择Radio单选组的第二个元素:`$('input[@name=items]').get(1).checked = true;` 7. **表单重置** - 使用`.reset()`方法,例如`$("form").each(function() { this.reset(); });`,可以重置所有表单内的字段至...

    正则表达式

    [xyz] 字符集(character set),匹配这个集合中的任一一个字符(或元字符) [^xyz] 不匹配这个集合中的任何一个字符 [\b] 匹配一个退格符 \b 匹配一个单词的边界 \B 匹配一个单词的非边界 \cX 这儿,X是一个控制符,/\...

    extjs帮助文档pdf版

    - `Ext.util.JSON.encode(data)`: 将JavaScript对象转换为JSON字符串。 - `Ext.util.JSON.decode(json)`: 将JSON字符串转换为JavaScript对象。 #### 19. Ext.util.Format 类 (P.20) - **概述**:提供了数据格式化...

    ExtAspNet_v2.3.2_dll

    -修正Expanded项和Checked项的状态在回发改变后不能保持的BUG。 -GetNodeById更名为FindNode,保持和FindControl一致命名。 -删除CheckedNodeIDArray属性,增加GetCheckedNodes和GetCheckedNodeIDs函数。 -...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -修正Expanded项和Checked项的状态在回发改变后不能保持的BUG。 -GetNodeById更名为FindNode,保持和FindControl一致命名。 -删除CheckedNodeIDArray属性,增加GetCheckedNodes和GetCheckedNodeIDs函数。 -...

Global site tag (gtag.js) - Google Analytics