1

我很难在总输入字段中显示变量的内容。基本上这段代码运行良好:

<script>
$(function() {
  $('#product-unit-val, #total-unit-sales, #number-reps').change(function() {
    var total = parseInt($("#product-unit-val").val()) *  
                parseInt($("#total-unit-sales").val()) * 
                parseInt($("#number-reps").val());
    if (!isNaN(total)) {
      $("#sales-val").html(total);
    }
  });
});
</script>

Average product unit value <input type="text" id="product-unit-val" /><br/>
Total unit sales value <input type="text" id="total-unit-sales" /><br/>
Number of reps <input type="text" id="number-reps" /><br/>
Total sales value <span id="sales-val"></span>$​​​

但是我想在输入字段中显示总值,例如:

<script>
$(function() {
  $('#product-unit-val, #total-unit-sales, #number-reps').change(function() {
    var total = parseInt($("#product-unit-val").val()) *  
                parseInt($("#total-unit-sales").val()) * 
                parseInt($("#number-reps").val());
    if (!isNaN(total)) {
      $("#sales-val").html(total);
    }
  });
});
</script>

Average product unit value <input type="text" id="product-unit-val" /><br/>
Total unit sales value <input type="text" id="total-unit-sales" /><br/>
Number of reps <input type="text" id="number-reps" /><br/>
Total sales value <input type ="text" id="sales-val"/>​

我错过了显而易见的事情吗?我似乎无法让这个工作。

非常感谢

4

1 回答 1

5

使用val更改输入的值:

$("#sales-val").val(total);

但要小心parseInt,始终指定 radix :

parseInt('09') == 0
parseInt('09', 10) == 9
于 2012-12-12T20:51:08.600 回答