如何在基本表td上计算项目数?

时间:2018-10-03 14:24:46

标签: javascript jquery

我使用此代码来计算添加车辆的次数。该代码未返回正确的值。我想计算在表的每一行中选择同一辆车的次数。

function calculateNumberOfVehicles(vehicleId) {
  var returnValue = 0;
  var table = $("#selected_students");
  table.find('tr').each(function(i, el) {

    var counter = 0;
    var rowId = $(this).attr('id');
    var actualId = rowId.substr(rowId.indexOf("_") + 1).trim();
    var addedVehicleId = $("#lockedVehicleId_" + actualId).text();
    if (parseInt(vehicleId) == parseInt(addedVehicleId)) {
      counter++
      returnValue = counter;
    }
  });
  return returnValue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody id="selected_students">
    <tr id="row_5ae958a4bb2c561a10003dcc">
      <td>Adam Zaffar</td>
      <td>L.K.G</td>
      <td>Male</td>
      <td id="lockedRoute_5ae958a4bb2c561a10003dcc">Karan Nagar</td>
      <td id="lockedStop_5ae958a4bb2c561a10003dcc">Lal chowk</td>
      <td id="lockedVehicle_5ae958a4bb2c561a10003dcc">JK 123</td>
      <td class="delete_row_dynamic"><button relation="5b962874d2ccda10ac003702" title="Delete" id="5ae958a4bb2c561a10003dcc" class="btn btn-danger table_btn btn-outline btn-sm deleteBtn"><i class="fa fa-trash"></i></button></td>
      <td id="lockedRouteId_5ae958a4bb2c561a10003dcc" class="hide">5b83dc30d2ccda0b60005c0f</td>
      <td id="lockedStopId_5ae958a4bb2c561a10003dcc" class="hide">b930e948-202b-697c-5615-58939cff2ac2</td>
      <td id="lockedVehicleId_5ae958a4bb2c561a10003dcc" class="hide">5b962874d2ccda10ac003702</td>
      <td id="lockedVehicleIdSeats_5ae958a4bb2c561a10003dcc" class="hide">3</td>
    </tr>
    <tr id="row_5b17cc27bb2c5633f400197c">
      <td>Adnan Mir</td>
      <td>L.K.G</td>
      <td>Male</td>
      <td id="lockedRoute_5b17cc27bb2c5633f400197c">New Jersey City</td>
      <td id="lockedStop_5b17cc27bb2c5633f400197c">City Center</td>
      <td id="lockedVehicle_5b17cc27bb2c5633f400197c">JK 123</td>
      <td class="delete_row_dynamic"><button relation="5b962874d2ccda10ac003702" title="Delete" id="5b17cc27bb2c5633f400197c" class="btn btn-danger table_btn btn-outline btn-sm deleteBtn"><i class="fa fa-trash"></i></button></td>
      <td id="lockedRouteId_5b17cc27bb2c5633f400197c" class="hide">5b7fb426d2ccda11fc005185</td>
      <td id="lockedStopId_5b17cc27bb2c5633f400197c" class="hide">aa61d076-30c1-31ff-c245-968178042e46</td>
      <td id="lockedVehicleId_5b17cc27bb2c5633f400197c" class="hide">5b962874d2ccda10ac003702</td>
      <td id="lockedVehicleIdSeats_5b17cc27bb2c5633f400197c" class="hide">3</td>
    </tr>
  </tbody>
</table>

2 个答案:

答案 0 :(得分:0)

尝试一下

var result = calculateNumberOfVehicles('5b962874d2ccda10ac003702');
console.log(result); // This will output the number in console.
function calculateNumberOfVehicles(vehicleId) {
  var returnValue = 0;
  var table = $("#selected_students");
  var counter = 0;
  table.find('tr').each(function(i, el) {
    var rowId = $(this).attr('id');
    var actualId = rowId.substr(rowId.indexOf("_") + 1).trim();
    var addedVehicleId = $("#lockedVehicleId_" + actualId).text();
    if (parseInt(vehicleId) == parseInt(addedVehicleId)) {
      counter++
      returnValue = counter;
    }
  });
  return returnValue;
}

答案 1 :(得分:0)

尝试一下:

     function calculateNumberOfVehicles(vehicleId)
   { 
     var returnValue = 0;
              var table = $("#selected_students");
              table.find('tr').each(function (i, el) {
                 var rowId = $(this).attr('id');
                 var actualId = rowId.substr(rowId.indexOf("_") + 1).trim();
                 var addedVehicleId = $("#lockedVehicleId_"+actualId).text();
                 if(parseInt(vehicleId) == parseInt(addedVehicleId))
                 {
                    returnValue++;                        
                 }
              });
    return returnValue;

   }