温馨提示:本文翻译自stackoverflow.com,查看原文请点击:jquery - Using .each() inside of a function

jquery - 在函数内部使用.each()

发布于 2020-03-27 11:25:38

我正在尝试建立一个函数,当inputs在内更改两个时,计算总数.item我希望inputs在每个上都发生这种情况.item,但是目前,我设置calculateTotal()函数的方式无法在每个.item运行keyup

function calculateTotal() {
  $(".item").each(function() {
    var item = $(this);
    var qty = item.find(".qty").val();
    var price = item.find(".cost").val();
    var total = Number(qty) * Number(price);
    $(".totalprice").text(total);
  });
}
calculateTotal();
$(".input--item").keyup(function() {
  calculateTotal();
});
.item {
  display: flex;
  align-items: center;
}

.wrap {
  border: 2px solid #eee;
  display: flex;
  flex-direction: column
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="12" maxlength="3" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="55" maxlength="4" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="40" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="79" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>

查看更多

查看更多

提问者
Kyle Underhill
被浏览
58
Rubix's Mind 2019-07-03 22:58

采用 item.find(".totalprice").text(total)

function calculateTotal() {
  $(".item").each(function() {
    var item = $(this);
    var qty = item.find(".qty").val();
    var price = item.find(".cost").val();
    var total = Number(qty) * Number(price);
    item.find(".totalprice").text(total)
    
  });
}
calculateTotal();
$(document).keyup('.input--item',function() {
  calculateTotal();
});
.item {
  display: flex;
  align-items: center;
}

.wrap {
  border: 2px solid #eee;
  display: flex;
  flex-direction: column
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="12" maxlength="3" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="55" maxlength="4" />
    </div>
  </div>
 <span class="totalprice"></span><br>
</div>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="40" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="79" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>

发布
问题

分享
好友

手机
浏览

扫码手机浏览