使用Jquery将Ropani-Anna转换为sq.ft的四个输入字段的面积计算

时间:2019-07-17 11:48:02

标签: javascript jquery

我有四个输入字段。我想将ropani,anna,paisa和dam转换为平方英尺。我无法转换它。我想计算四个输入字段的值并希望在另一个字段中显示。请帮助我。

$(document).on("change", ".ropani", function() {
      var area = 0;
      $(‘.ropani’).on(‘keyup’, function() {
            var ropani = $(".ropani").val();
            var aana = $(".aana").val();
            var paisa = $(".paisa").val();
            var dam = $(".dam").val();
            area += ropani * 5476.0000;
            area += aana * 342.2500;
            area += paisa * 85.5625;
            // if (parseFloat(daam) < 4) {
            area += daam * 21.390625;
            //s }

            $(“.total”).html(area);
          };
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type=”text” class=”ropani” />
<input type=”text” class=”anna” />
<input type=”text” class=”paisa” />
<input type=”text” class=”dam” />
<input type=”text” class=”total” />

2 个答案:

答案 0 :(得分:1)

不是向change添加input.ropani事件。将keyup事件添加到除inputs之外的所有.total中。使用.val()来更改input的值。

$("input:not(.total)").on("keyup", function() {
  var area = 0;
  var ropani = +$(".ropani").val() || 0;
  var aana = +$(".aana").val() || 0;
  var paisa = +$(".paisa").val() || 0;
  var dam = +$(".dam").val() || 0;
  area += ropani * 5476.0000;
  area += aana * 342.2500;
  area += paisa * 85.5625;
  area += dam * 21.390625;
  console.log(area); 
  $('.total').val(area);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" class="ropani" />
<input type="number" class="anna" />
<input type="number" class="paisa" />
<input type="number" class="dam" />
<input type="number" class="total" />

答案 1 :(得分:1)

代码中有一些错误。假设您希望在任何字段更改时在第五个字段中输入值,请考虑以下因素:

$('input:not(.total)').on('keyup', function() {
  var area = 0;
  var ropani = $(".ropani").val();
  var aana = $(".aana").val();
  var paisa = $(".paisa").val();
  var daam = $(".dam").val();
  area += ropani * 5476.0000;
  area += aana * 342.2500;
  area += paisa * 85.5625;
  // if (parseFloat(daam) < 4) {
  area += daam * 21.390625;
  //s }

  $(".total").val(parseFloat(area));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
ropani<input type="text" class="ropani" /><br> anna
<input type="text" class="aana" /><br> paisa
<input type="text" class="paisa" /><br> dam
<input type="text" class="dam" /><br> total
<input type="text" class="total" /><br>


@Fallerreaper's条建议之后进行了更改:

var obj = {};
$('input:not(.total)').on('keyup', function(event) {
  var area = 0;
  if (parseFloat(event.target.value) == NaN) return;
  obj[event.target.classList[0]] = event.target.value;
  obj.ropani = obj.ropani ? obj.ropani : $(".ropani").val();
  obj.aana = obj.aana ? obj.aana : $(".aana").val();
  obj.paisa = obj.paisa ? obj.paisa : $(".paisa").val();
  obj.daam = obj.daam ? obj.daam : $(".dam").val();
  area += obj.ropani * 5476.0000 + obj.aana * 342.2500 + obj.paisa * 85.5625 + obj.daam * 21.390625;

  $(".total").val(parseFloat(area));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
ropani<input type="text" class="ropani" /><br> anna
<input type="text" class="aana" /><br> paisa
<input type="text" class="paisa" /><br> dam
<input type="text" class="dam" /><br> total
<input type="text" class="total" /><br>