基于同一表格行中输入框的最高值查找元素

时间:2016-04-06 15:40:08

标签: javascript jquery

我有一个包含三列的表格。

  1. 第1列有输入框。
  2. 第2列包含"转换"将其值与输入框(第1列)中的值相乘的数字
  3. ,结果在第3栏中输出。
  4. 它看起来像这样:

    5    2   10
    1    1    1
    2    1    2
    3    2    6
    

    如何根据同一行中的最高输入框值获取第3列的值?

    到目前为止我的代码:

    HTML

    <table border="1px">
        <tbody id="mbtbody">
          <tr>
              <td><input class="weightinputclass" type="number" value="0"></td>
              <td class="conversionclass">5</td>
              <td class="totals"></td>
          </tr>
          <tr>
              <td><input class="weightinputclass" type="number" value="0"></td>
              <td class="conversionclass">1</td>
              <td class="totals"></td></tr>
          <tr>
              <td><input class="weightinputclass" type="number" value="0"></td>
              <td class="conversionclass">1</td>
              <td class="totals"></td>
          </tr>
        </tbody>
    </table>
    

    的jQuery

    $("#btn").click(function(){
       var rows = $("#mbtbody").children("tr");
       var total = 0;
         rows.each(function(idx, row) {
           var $row = $(row);
           total += parseInt($row.find('input.weightinputclass').val());
           $("#totalweight").html(total);       
         });
    })
    
    $(document).on('input', '.weightinputclass', function () {
       var $row = $(this).closest("tr");
       var weight = $row.find("input.weightinputclass").val();
       var conversion= $row.find(".conversionclass").html();
       $row.find(".totals").text(weight*conversion);
    })
    

    小提琴: https://jsfiddle.net/rdawkins/9fyLqfgr/16/

1 个答案:

答案 0 :(得分:1)

这是我找到的解决方案:

的Javascript

$("#btn").click(function(){
   var rows = $("#mbtbody").children("tr");
   var total = 0;
     rows.each(function(idx, row) {
       var $row = $(row);
       total += parseInt($row.find('input.weightinputclass').val());
       $("#totalweight").html(total);       
     });

     var currentMax = 0;
     var currentMaxEl;
     $('.weightinputclass').each(function (idx, element) {
        var elVal = parseInt($(element).val());

        if(elVal > currentMax) {
          currentMax = elVal;
          currentMaxEl = $(element);
        }
     });
     var maxWeight = currentMaxEl.parent().parent().children('.totals').first().html();
     $('#highestinput').html(2000 - maxWeight);
})

$(document).on('input', '.weightinputclass', function () {

var $row = $(this).closest("tr");
var weight = $row.find("input.weightinputclass").val();
var conversion= $row.find(".conversionclass").html();
$row.find(".totals").text(weight*conversion);
});

小提琴

https://jsfiddle.net/9v6j8qub/2/