用javascript计算

时间:2017-08-22 09:23:31

标签: javascript php

我找到了这个脚本,以及我现在需要学习的内容。

Similar code

我想从PHP获取一些数据并使用该日期进行计算!

但显然我不明白该怎么做!

$(window).load(function(){
    $('div#cont-sum-fields').on('change', 'input', function() {
//        var total =  (parseInt($( "#cont-sum-1" ).val()) +  parseInt($( "#cont-sum-2" ).val())) * parseInt($( "#cont-sum-3" ).val()) ;
 var printsmall = 6;
//var pricesmall = <? echo $priceAUD[4][2];?>; // output 6.0
        var total =  (parseInt($( "#cont-sum-1" ).val()) +  parseInt($( "#cont-sum-2" ).val())) * pricesmall) ;
        $('#cont-sum-fields').find('#total-cont-sum').val(total);
        find('pricesmall').val(pricesmall);
    });
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <div id='cont-sum-fields'>
    (<input type="number" id="cont-sum-1" /> +
    <input type="number" id="cont-sum-2" />) x
    <input id="pricesmall" type="number" disabled /> =
    <input id="total-cont-sum" type="number" disabled />
  </div>  

关于我想要完成的事情的一点概述:

第一个数字+第二个数字* php或var printsmall = total!

1 个答案:

答案 0 :(得分:0)

以下是我认为您想要的方式修改的代码。

请注意缩进等,它使阅读代码更容易。

&#13;
&#13;
$(window).load(
  function(){
    var printsmall = 6;
    $('#pricesmall').val(printsmall);
    $('#cont-sum-fields').on('change', 'input', 
      function() {
        var 
          s1 = $('#cont-sum-1').val(),
          s2 = $('#cont-sum-2').val(); 
        if (s1 && s2) {
          $('#total-cont-sum').val(
            (parseFloat(s1) + parseFloat(s2)) * printsmall);
        }
      }
    )
  }
);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <div id='cont-sum-fields'>
    (<input type="number" id="cont-sum-1" /> +
    <input type="number" id="cont-sum-2" />) x
    <input id="pricesmall" type="number" disabled /> =
    <input id="total-cont-sum" type="number" disabled />
  </div>
&#13;
&#13;
&#13;