jQuery 每个 ID 的 min(最小值)

jQuery min (smallest value) for each individual ID

我在一个页面上有 2 个以上的旅行价格,每个旅行(div 容器)都应该有一个起始价格(最小值)。

所以我写了这个,它正在工作...

jQuery(function($) {
    var vals = $('.prices').map(function () {
    return parseInt($(this).text(), 10) ? parseInt($(this).text(), 10) :  null;
    }).get();

// then find their minimum
    var min = Math.min.apply(Math, vals);

// tag any cell matching the min value
    $('.prices').filter(function () {
    return parseInt($(this).text(), 10) === min;
    });

   $('.start-price').text(min);
});

但显然该脚本从页面上的所有价格(所有容器)中取最小值。它应该从每次(每次)旅行中取最小值。容器的 ID 可以动态生成。 我怎样才能做到这一点?

确保每个行程 div 都有一个 class(比方说 .travel)并为每个行程执行计算。

jQuery(function($) {      
    $(".travel").each(function() {

      var $prices = $(this).find(".prices");
      var vals = $prices.map(function () {
        return parseInt($(this).text(), 10) ? parseInt($(this).text(), 10) :  null;
      }).get();

      // then find their minimum
      var min = Math.min.apply(Math, vals);

      // tag any cell matching the min value
      $prices.filter(function () {
        return parseInt($(this).text(), 10) === min;
      });
      $(this).find('.start-price').text(min);
    });       
});