javascript - 如何从 while 循环获取当前价格

标签 javascript php jquery html

各位 friend 大家好,

正如您在下面看到的,我有 Jquery 代码和 PHP/HTML 代码。您可以在 Jquery 代码中看到价格变量。控制台日志打印所有产品的所有价格,但我想要当前数量点击 .update-quantity 值中的当前价格值。

.closest 适用于数量形式。我尝试对价格表执行相同的操作,但我无法弄清楚。

抱歉,我是一个了解如何使用 jQuery 从 while 循环中获取值的初学者

//Jquery函数

$(".update-quantity").click(function(e){
  var id       = $(this).data('id');
  var quantity = $(this).closest('.update-quantity-form').find('.cart-quantity').text();
  var price    = $('.update-price-price-form').find('.cart-price-price').text();
  console.log("Price: "+price);
});

//PHP HTML代码

  extract($row);

  // update quantity WORKS
  echo "<div class='update-quantity-form'>";
  <a class='update-quantity' data-id='{$id}'>-</a>
  <div class='cart-quantity'>{$quantity}</div>
  <a class='update-quantity' data-id='{$id}'>+</a>
  </div>";

  // update price DOESNT work
  echo "<div class='update-price-price-form'>";
  echo "&#8364;<span class='cart-price-price'>$price</span>";
  echo "</div>";
}

我的目标是使用价格值,然后使用具有数量值的 AJAX php 文件进行计算。然后我将显示价格和小计,无需刷新页面。

感谢您的宝贵时间,非常感谢!

最佳答案

您需要找到 .update-quantity-form 的同级并从那里获取文本:

$(".update-quantity").click(function(e){
  var qf       = $(this).closest('.update-quantity-form');

  var id       = $(this).data('id');
  var quantity = qf.find('.cart-quantity').text();
  var price    = qf.siblings('.update-price-price-form').find('.cart-price-price').text();
  console.log("Price: " + price);
});

关于javascript - 如何从 while 循环获取当前价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61306631/

相关文章:

php - 匹配以 pics.domain.com 开头的所有图像

javascript - OnClick 元素用 JS 填充数组

javascript - Django-Ajax(Jquery)删除功能问题

php - Stripe "No such token: "错误

javascript - scribe js 错误 - console.log 未定义

javascript - 使用 jquery 和 php 动态禁用一些复选框选项

javascript - For 循环 - 语法错误

php - Laravel First 或 New

php - 循环遍历以 JSON 编码的 PHP 数组

javascript - Sequelize : Include model of through. 属性