Reputation: 15866
javascript code:
function getCheckbox(name,id){
var check = 0;
var deger = "option-checkbox-"+id;
var dom = document.getElementById(deger);
if( dom.checked )
check += parseFloat( dom.value );
else check -= parseFloat( dom.value );
return check.toFixed(2);**//right value.(12.45)**
}
function getRadio(name,id) {
var radio=0;
var deger="option-radio-"+id;
var radio="option["+name+"]";
var selectedli = document.getElementsByName(radio);
var degerli = document.getElementById(deger);
if( selectedli.checked )
radio = parseFloat( degerli.value );
else radio = parseFloat( degerli.value )
return radio.toFixed(2)**//right value.(42.00)**
}
function getSelected(name,id) {
var selectedid="option["+name+"]";
var selecte = 0;
var selected = document.getElementById( selectedid );
selecte = parseFloat(selected.value);
return selecte.toFixed(2);**//right value.(-12.29)**
}
function toplam(name,id) {**//not expected(0)**
var toplan = 0;
toplan = getSelected(name,id)+getRadio(name,id)+getCheckbox(name,id);
$( '#toplam_goster' ).fadeOut(200, function(){
document.getElementById("toplam_goster").innerHTML = toplan;
});
$( '#toplam_goster' ).fadeIn(200);
}
.In the last function return 0. How can I fix it.
Thanks.
Upvotes: 0
Views: 206
Reputation: 38345
There are a few problems with this particular function:
function getRadio(name,id)
{
var radio=0;
var deger="option-radio-"+id;
var radio="option["+name+"]";
var selectedli = document.getElementsByName(radio);
var degerli =document.getElementById(deger);
if(selectedli.checked) {
radio = parseFloat(degerli.value);
}
else {
radio = parseFloat(degerli.value)
}
return radio.toFixed(2)**//right value.(42.00)**
}
You're declaring the radio
variable twice - originally with a value of 0
and then as a string.
You're calling document.getElementsByName()
which returns an array of elements, then later on doing selectedli.checked
; the array won't have a checked property. If you're expecting it to only return a single radio
element, you should be testing selectedli[0].checked
.
You have an if
statement that executes the same code regardless of whether the condition evaluates to true. This is either a mistake on your part or the if
statement can be done away with entirely.
Upvotes: 1
Reputation: 4666
return selecte.toFixed(2); // this returns string and addition might return 0
either return number, i.e. return selecte*1 or convert to number before adding, i.e.
toplan=getSelected(name,id)*1+getRadio(name,id)*1+getCheckbox(name,id)*1;
Upvotes: 3