dotif64826 2013-08-24 11:59
浏览 18
已采纳

Javascript计算问题

I have this function but i have issue with it.

function calc(id)
{
var price = document.getElementById("hidden-output-"+id);
var amount = document.getElementById("input-"+id);

if(price.value=="")
{
price.value = 0;
}
if(b.value=="")
{
amount.value = 1;
}

var total = amount.value * price.value;
document.getElementById("output-"+id).value=total;
}

It's not working and I don't know why I am new to javascript.

This is the HTML and PHP

<?php




foreach($category->fetch_category() as $data){

    $id = $data->id;

    echo"<tr>";
    echo"<td>";
    echo"<center>";
    echo"<input type=\"hidden\" value=\"$data->id\" />";
    echo"<p><strong>".$data->cat_name."</strong></p>";
    echo"</center>";
    echo"</td>";
    echo"<td>";
    echo"<center>";


    echo"<select>";
    foreach($products->fetch_products($id) as $product)
    {
        $price = $product->price;
    echo"<option value = $product->id onclick=\"UpdateRecord($product->id,$data->id)\">$product->p_name</option>";
    }
    echo"</select>";

    echo"</center>";
    echo"</td>";

    echo"<td>";
    echo"<center>";
    echo"<input class=\"input-mini\" id=input-$data->id onkeyup=\"calc($data->id)\" type=\"text\" value=\"1\"/>";
    echo"<input type=\"hidden\" id=\"$product->id\" value=\"$product->quantity\" />";
    echo"</center>";
    echo"</td>";
    $i = 0;
    foreach($products->fetch_products($id) as $pricer){
    if ($i==1) break;    

    echo"<td>";

    echo"<center>";
    echo("<div id=\"output-$data->id\" ><p>$pricer->price</p></div>");
    echo"<input type=\"hidden\" id=\"hidden-output-$data->id\" value=\"$pricer->price\"/>";
    echo"</center>";
    echo"</td>";
    $i++;
    }
    echo"</tr>";
}

?>

I am triyng to calculate the price of each field amount * price then to SUM all price fields and display result in

<td>Total : <input class="span1" type="text" id="total_calc" readonly="true" /></td>

I also tried these variants :

function calc3(id) {

   var price = document.getElementById('hidden-output-'+id);
   var numberField = document.getElementById('input-'+id);
   numberField.onkeyup = numberField.onpaste = function() {
      if(this.value.length == 0) {
         document.getElementById('output-'+id).innerHTML = '';
         return;
      }
      var number = parseInt(this.value);
      if(isNaN(number)) return;
      document.getElementById('output-'+id).innerHTML = number * price;
   };
   numberField.onkeyup(); 
};

// This one always return NaN

This is the project : http://tinypic.com/view.php?pic=34473mp&s=5

  • 写回答

1条回答 默认 最新

  • dongxian0320 2013-08-24 12:20
    关注
    if(b.value=="")
    {
      amount.value = 1;
    }
    

    What is b.value in your calc function. This is not defined that is why throws error and script is not executed.

    In your calc3 function. You need to modify following line

    if(isNaN(number)) return; to number = isNaN(number) ? 0 : number

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办