获得radiobox值 [英] Get radiobox value
问题描述
您好,
我想检查用户是否从radiobox组中选择一个。如何在javascript中获得radiobox实际值
。在值字段中,我看到
的默认值,但我怎么知道它是否真的被选中了?
谢谢!
>
< input name =" menu_order" id =" menu_order1"类型= QUOT;无线电" value =" 1"> one< br>
< input name =" menu_order" id =" menu_order2"类型= QUOT;无线电" value =" 2"> two< br>
我想要检查用户是否从radiobox组中选择一个。
我会使用术语单选按钮组。
检查:
< html>
< head>
< title>单选按钮值< / title>
< script type =" text / javascript">
函数验证()
{
alert(我们在函数验证中);
var checkedButton;
var allOK = false;
var x = document.forms [" myForm"];
//找出按下哪个单选按钮
if(x.receiveVia [ 0] .checked == true)
{
checkedButton = x.receiveVia [0] .value;
allOK = true;
}
else if(x.receiveVia [1] .checked == true)
{
checkedBu tton = x.receiveVia [1] .value;
allOK = true;
}
else
{
提醒(按钮未按下);
checkedButton ='''';
allOK = false;
}
alert(" checkedButton =" + checkedButton +
" ;; allOK =" + allOK);
返回allOK;
}
< / script>
< / head>
< body>
< p>请试试我们的表格。 < / p>
< form name =" myForm"
action =" http://www.nonamedomain.com"
method =" POST"
onsubmit =" alert(''submitting''); return validate();">
< p>< input type =" radio"名称= QUOT; receiveVia" value =" printed">& nbsp;已打印
宣传册< / p>
< p>< input type =" radio"名称= QUOT; receiveVia"值= QUOT; byEmail" ">& nbsp; Via
电子邮件< / p>
< / p>
< p> ;< input type =" submit" value =" Submit">
< / p>
< / form>
< / body>
< / html>
Robert
Robert写道:
< blockquote class =post_quotes>
我想检查用户是否从radiobox组中选择一个。
我会使用术语单选按钮组。
检查:
< html>
< head>
< title>单选按钮值< / title>
< script type =" text / javascript">
功能验证()
{
警告(我们在功能中验证);
var checkedButton;
var allOK = false;
var x = document.forms [" myForm"];
//找出哪个按下单选按钮
如果(x.receiveVia [0] .checked == true)
{
checkedButton = x.receiveVia [0] .value;
allOK = true;
}
否则if(x.receiveVia [1] .checked == true)
{
checkedButton = x.receiveVia [1] .value;
allOK = true;
}
其他
{
警告(未按下按钮);
checkedButton ='''';
allOK = false;
}
alert(" checkedButton =" + checkedButton +
" ;; allOK =" + allOK);
返回allOK;
}
< / script>
< / head>
< body>
< p>请试试我们的表格。< / p>
< form name =" myForm"
action =" http://www.nonamedomain.com"
method =" POST"
onsubmit =" alert(''submitting''); return validate();" >
< p>< input type =" radio"名称= QUOT; receiveVia" value =" printed">& nbsp; Printed
宣传册< / p>
< p>< input type =" radio"名称= QUOT; receiveVia"值= QUOT; byEmail" ">& nbsp; Via
电子邮件< / p>
< / p>
< p>< input type =" submit" value =" Submit">
< / p>
< / form>
< / body> >< / html>
Robert
更适用于任何一组单选按钮的通用解决方案:
>
函数getRadioValue(formName,radioInputName){
var f = document.forms [formName];
if(f){
f = f.elements [radioInputName];
}
if(f){
if(typeof f.length =='' undefined''){
f = [f];
}
for(var i = 0; i< f.length; i ++ ){
if(f [i] .checked){
return f [i] .value;
}
}
}
返回null;
}
函数验证(f){
if(getRadioValue(''myForm'',''receiveVia'')== null){
alert(" you haven'' t选择了\接收Via \。;
返回false;
}
返回true;
}
-
| Grant Wagner< gw ***** @ agricoreunited.com>
*客户端Javascript和Netscape 4 DOM参考可从以下位置获得:
*
http:// devedge .netscape.com / library / ... ce / frames.html
* Internet Explorer DOM参考资料:
*
http://msdn.microsoft.com/研讨会/ a ... ence_entry.asp
* Netscape 6/7 DOM参考资料于:
* http://www.mozilla.org/docs/dom/domref/
*为Netscape 7 / Mozilla升级JavaScript的提示
* http://www.mozil la.org/docs/web-deve...upgrade_2.html
Robert写道:
谁写道?请提供适当的归属。
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv我要检查,如果用户选择一个来自radiobox组
我会使用术语单选按钮组。
检查:
[...]
功能验证()
{
alert(我们在函数验证中);
var checkedButton;
var allOK = false;
var x = document.forms [" myForm"];
//找出按下哪个单选按钮
if(x.receiveVia [0] .checked == true)
{
checkedButton = x.receiveVia [0] .value;
allOK = true;
}
如果(x.receiveVia [1] .checked == true)
{
checkedButton = x .receiveVia [1] .value;
allOK = true;
}
{
警告(按钮未按下);
checkedButton ='''';
全部OK = false;
}
alert(" checkedButton =" + checkedButton +
" ;; allOK =" + allOK);
返回allOK;
}
该代码不能很好地扩展。单选按钮,即输入按钮。元素
类型=" radio"一组人需要具有相同的名称。在DOM中,
导致元素集合(HTMLCollection对象),因此
以下更好:
函数getCheckedRadio(
/ ** @argument HTMLFormElement * / oForm,
/ ** @argument string * / sGroup)
/ **
* @author
*版权所有(C)2004 Thomas Lahn< dh ****** @ PointedEars.de>
* @返回
* null,如果@ {(oForm)}无效或没有这样的@ {(sGroup)}< br>
* false,如果没有收音机@ {(sGroup)}的按钮被选中< br>
*对已检查的单选按钮的HTMLInputElement引用否则
* /
{
var result = null,e,ig;
if(oForm
&&(e = oForm.elements)>
&&(ig = e [sGroup]))
{
result = false;
for(var i = 0,len = ig.length,io = null;
i< len;
i ++)
{
if((io = ig [i ])。已检查)
{
结果= io;
休息;
}
}
}
返回结果;
}
var x;
if((x = getCheckedRadio(document.forms [0]," receiveVia")))
{
alert( checkedButton =" + x $ value};
}
else if(x == false)
{
alert( 没有选中按钮。;
}
其他
{
alert("没有这样的表格或按钮组。);
}
PointedEars
Hello,
I want to check if user select one from the radiobox group. How can I get
the radiobox actual value in javascript. In the value field I see the
default value, but how can I know if it really was selected?
Thanks!
<input name="menu_order" id = "menu_order1" type="radio" value="1">one<br>
<input name="menu_order" id = "menu_order2" type="radio" value="2">two<br>
I want to check if user select one from the radiobox group.
I''d use the term radio button group.
Examine:
<html>
<head>
<title>Radio button value</title>
<script type="text/javascript">
function validate()
{
alert("We are in function validate");
var checkedButton;
var allOK= false;
var x = document.forms["myForm"];
// Figure out which radio button was pressed
if (x.receiveVia[0].checked == true)
{
checkedButton = x.receiveVia[0].value;
allOK = true;
}
else if (x.receiveVia[1].checked == true)
{
checkedButton = x.receiveVia[1].value;
allOK = true;
}
else
{
alert("button not pressed");
checkedButton = '''';
allOK = false;
}
alert("checkedButton = " + checkedButton +
"; allOK = " + allOK);
return allOK;
}
</script>
</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="http://www.nonamedomain.com"
method="POST"
onsubmit="alert(''submitting'');return validate();">
<p><input type="radio" name="receiveVia" value="printed"> Printed
brochure</p>
<p><input type="radio" name="receiveVia" value="byEmail" "> Via
Email</p>
</p>
<p><input type="submit" value="Submit">
</p>
</form>
</body>
</html>
Robert
Robert wrote:
I want to check if user select one from the radiobox group.
I''d use the term radio button group.
Examine:
<html>
<head>
<title>Radio button value</title>
<script type="text/javascript">
function validate()
{
alert("We are in function validate");
var checkedButton;
var allOK= false;
var x = document.forms["myForm"];
// Figure out which radio button was pressed
if (x.receiveVia[0].checked == true)
{
checkedButton = x.receiveVia[0].value;
allOK = true;
}
else if (x.receiveVia[1].checked == true)
{
checkedButton = x.receiveVia[1].value;
allOK = true;
}
else
{
alert("button not pressed");
checkedButton = '''';
allOK = false;
}
alert("checkedButton = " + checkedButton +
"; allOK = " + allOK);
return allOK;
}
</script>
</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="http://www.nonamedomain.com"
method="POST"
onsubmit="alert(''submitting'');return validate();">
<p><input type="radio" name="receiveVia" value="printed"> Printed
brochure</p>
<p><input type="radio" name="receiveVia" value="byEmail" "> Via
Email</p>
</p>
<p><input type="submit" value="Submit">
</p>
</form>
</body>
</html>
Robert
More generic solution that works with any group of radio buttons:
function getRadioValue(formName, radioInputName) {
var f = document.forms[formName];
if (f) {
f = f.elements[radioInputName];
}
if (f) {
if (typeof f.length == ''undefined'') {
f = [ f ];
}
for (var i = 0; i < f.length; i++) {
if (f[i].checked) {
return f[i].value;
}
}
}
return null;
}
function validate(f) {
if (getRadioValue(''myForm'', ''receiveVia'') == null) {
alert("You haven''t selected a \"Receive Via\".");
return false;
}
return true;
}
--
| Grant Wagner <gw*****@agricoreunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Robert wrote:
Who wrote that? Please provide proper attribution.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvI want to check if user select one from the radiobox group.
I''d use the term radio button group.
Examine:
[...]
function validate()
{
alert("We are in function validate");
var checkedButton;
var allOK= false;
var x = document.forms["myForm"];
// Figure out which radio button was pressed
if (x.receiveVia[0].checked == true)
{
checkedButton = x.receiveVia[0].value;
allOK = true;
}
else if (x.receiveVia[1].checked == true)
{
checkedButton = x.receiveVia[1].value;
allOK = true;
}
else
{
alert("button not pressed");
checkedButton = '''';
allOK = false;
}
alert("checkedButton = " + checkedButton +
"; allOK = " + allOK);
return allOK;
}
That code does not scale well. Radio buttons, i.e. "input" elements
with type="radio" of a group need to have the same name. In the DOM,
that results in an elements collection (HTMLCollection object), thus
the following is better:
function getCheckedRadio(
/** @argument HTMLFormElement */ oForm,
/** @argument string */ sGroup)
/**
* @author
* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>
* @returns
* null, if @{(oForm)} is invalid or there is no such @{(sGroup)}<br>
* false, if no radio button of @{(sGroup)} is checked<br>
* A HTMLInputElement reference to the checked radio button otherwise
*/
{
var result = null, e, ig;
if (oForm
&& (e = oForm.elements)
&& (ig = e[sGroup]))
{
result = false;
for (var i = 0, len = ig.length, io = null;
i < len;
i++)
{
if ((io = ig[i]).checked)
{
result = io;
break;
}
}
}
return result;
}
var x;
if ((x = getCheckedRadio(document.forms[0], "receiveVia")))
{
alert("checkedButton = " + x.value);
}
else if (x == false)
{
alert("No button is checked.");
}
else
{
alert("No such form or button group.");
}
PointedEars
这篇关于获得radiobox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!