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には注意してください。常に、基数を指定してください。

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