-1

i'm trying to calculate value on listbox, but my code didn't work this is my javascript code :

var val=document.getElementById('inh').value;
var temp=val.split("");

   function sum(){
    var val = document.getElementById('inh').value;
    var temp = val.split(" ");

    var total = 0;
    var v;
    for(var i = 0; i < temp.length; i++) {
        v = parseFloat(temp[i]);
        if (!isNaN(v)) total += v;
    }

    document.getElementById('resultSum').innerHTML=total;

}

this is the html code :

<label for="ttotal"></label>
        <input type="text" name="ttotal" id="ttotal"  onkeyup="sum()"  />

and

  <select name="inh[<?php echo $no; ?>]">
     <option value="">--Pilih--</option>
    <option value="1" <?php if($datatampil['dosis_inh_perhari']==1) echo 'selected'; ?>>1 tablet</option>
    <option value="2" <?php if($datatampil['dosis_inh_perhari']==2) echo 'selected'; ?>>2 tablet</option>
    <option value="3" <?php if($datatampil['dosis_inh_perhari']==3) echo 'selected'; ?>>3 tablet</option>
  </select>

thank's

4

3 に答える 3

0
function sum( el ){ var val = document.getElementById( el ).value;
 var temp = val.split(" ");
 var total = 0; 
var v;
 for(var i = 0; i < temp.length; i++) {
 v = parseFloat(temp[i]); 
if (!isNaN(v)) total += v;
 }
 document.getElementById('resultSum').innerHTML=total; 
}

<input type="text" name="ttotal" id="ttotal" onkeyup="sum('[<?php echo $no; ?>]')" />
于 2013-07-29T13:33:29.763 に答える