-1

我正在尝试计算列表框上的值,但我的代码不起作用这是我的 javascript 代码:

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;

}

这是html代码:

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

  <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>

谢谢

4

3 回答 3

0

我的代码确实有效,但是当我在列表框上更改我的选择时,它没有计算出新的更改,为什么?

function jumlah(n){
    var nilai = 0;
        for( var i = 1; i<=n; i++ )
        {
            var a = document.getElementById("inh["+i+"]");
                if(a.value != "")
            {
                nilai = nilai + parseInt(a.value);
            }
        }
        //alert(n);
        document.getElementById("txtjumlah").value = nilai;
        //alert(nilai);
    }

这是列表框:

<select name="inh[<?php echo $no; ?>]" id="inh[<?php echo $no; ?>]" onchange="jumlah(<?php echo $count; ?>);">
            <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>

这是输出计算数据的文本:

<input type="text" name="txtjumlah" id="txtjumlah" value="" readonly="readonly" style="width:30px;" />

感谢您的帮助 :)

于 2013-07-31T03:51:45.913 回答
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 回答
0

代替

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

有了这个:

<input type="text" name="ttotal" id="ttotal"  onkeyup="sum('[<?php echo $no; ?>]')"  />

Javascript函数

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;
}
于 2013-07-29T06:04:08.657 回答