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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c