简体   繁体   English

根据同一表行中输入框的最大值查找元素

[英]Find element based on highest value of inputbox in same table row

I have a table with three columns. 我有一张三列的桌子。

  1. Column 1 has inputboxes. 第1列具有输入框。
  2. Column 2 contains a "conversion" number which multiplies it's value with the value in the inputbox (column 1) 第2列包含一个“转换”数字,该数字将其值乘以输入框(第1列)中的值
  3. and the result is outputted in column 3. 并将结果输出到第3列。

It would look something like this: 它看起来像这样:

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

How can I get column 3's value based on the highest inputbox value in the same row? 如何根据同一行中最高的输入框值获取第3列的值?

My code so far: 到目前为止,我的代码:

HTML 的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 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);
})

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

Here is a solution I found: 这是我找到的解决方案:

Javascript Java脚本

$("#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);
});

Fiddle 小提琴

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM