-2

我有这样的表格

area: <input type="text" id="area" name="area" class="area"  value="0"/>
Price per square meter: <input type="text" id="square" name="square" class="square"       value="0"/>
Total Price: <span class="total"></span>
25% of total price:  <span class="25total"></span>

和一个脚本

<script type="text/javascript">
 $.fn.priceTotal = function() {
var valueArea = $("#area").val(); 
var valueAreaFinal = parseFloat(valueArea, 10);

var valueSquare = $("#square").val(); 
var valueSquareFinal = parseFloat(valueSquare, 10);

var TotalPriceFinal = valueAreaFinal * valueSquareFinal;
return TotalPriceFinal;
    };


    $(document).ready(function() {
$('input.area').bind('keyup', function() {
    $('span.total').html( $('input.price').priceTotal() );
});
$('input.square').bind('keyup', function() {
    $('span.total').html( $('input.price').priceTotal() );
});
    }); 

</script>

使用此表格,当我输入每平方米的面积和价格时,总价格会自动计算。我现在的问题是,如何在另一个名为 25total 的跨度中计算总价的 25%

4

2 回答 2

0
<script type="text/javascript">
 $.fn.priceTotal = function() {
var valueArea = $("#area").val(); 
var valueAreaFinal = parseFloat(valueArea, 10);

var valueSquare = $("#square").val(); 
var valueSquareFinal = parseFloat(valueSquare, 10);

var TotalPriceFinal = valueAreaFinal * valueSquareFinal;
return TotalPriceFinal;
    };


    $(document).ready(function() {
$('input.area').bind('keyup', function() {
    $('span.total').html( $('input.price').priceTotal() );
    $('span.25total').html( $('input.price').priceTotal() / 4 );
});
$('input.square').bind('keyup', function() {
    $('span.total').html( $('input.price').priceTotal() );
    $('span.25total').html( $('input.price').priceTotal() / 4 );
});
    }); 

</script>
于 2012-09-20T11:50:29.633 回答
0

考虑到您正在使用 keyup 事件,您将希望 priceTotal 尽可能高效,而不是每次调用时都创建变量。寻求这个解决方案:

  $.fn.priceTotal = function() {
        return parseFloat($("#area").val(), 10) * parseFloat($("#square").val(), 10)
    };


$(document).ready(function() {
    $('input.area, input.square').bind('keyup', function() {
      var priceTotal = $('input.price').priceTotal();
      $('span.total').html(priceTotal);
      $('.25total').text(parseInt(priceTotal) / 4));
   }
});

</script>
于 2012-09-20T11:52:08.943 回答