JavaScript code for check the value of radio button (jQuery.val(); alternative)
I've two radio buttons to select a type of IVA:
<input type="radio" value="18" checked="true" name="ivacontract"> 18%
<input type="radio" value="16" name="ivacontract"> 16%
And I've one function that will calculate a value depending of the value of the radio button with name "ivacontract". In jQuery I'll use a selector and .val(); to get the value but thi开发者_如何学Cs application (not coded by me) doesn't use jQuery so I don't know how to do it.
Any help?
Thank you in advance!
function getCheckedValues(objName)
{
var arr = new Array();
arr = document.getElementsByName(objName);
for(var i = 0; i < arr.length; i++)
{
var obj = document.getElementsByName(objName).item(i);
if(obj.checked)
{
alert(obj.value);
}
}
}
getCheckedValues("ivacontract");
Here is a working sample:
http://jsfiddle.net/eJBg9/1/
Try this:
function checkradio(fname,rname)
{
var radios=document[fname].elements[rname];
for(var i=0;i<radios.length;i++)
{
if(radios[i].checked)
return radios[i].value;
}
return false;
}
Where fname
is your form name and rname
is your radio buttons name that is in your code is ivacontract
.
Hope this helps.
Also, here is an example of getting and setting a value for the radio buttons.
http://www.somacon.com/p143.php
<input type="radio" id="radio1" value="18" checked="true" name="ivacontract"> 18%
<input type="radio" id="radio2" value="16" name="ivacontract"> 16%
if (document.getElementById('radio1').checked==true) )
{
/// Your Code ///
}
精彩评论