简体   繁体   English

如何遍历Bootstrap表并使用JQuery输入值

[英]How to iterate through a Bootstrap table and enter in values using JQuery

I am trying to iterate through a bootstrap table. 我试图通过引导表进行迭代。 I have created if statements so I can get my desired output. 我已经创建了if语句,因此可以获得所需的输出。

This is my desired output: 这是我想要的输出: 在此处输入图片说明

However, so far no value appears in the print cost and paper cost columns but the values appear on the first row if you take out the second row. 但是,到目前为止,如果您除去第二行,则在“打印成本”和“纸张成本”列中不会显示任何值,但是这些值会显示在第一行中。

My HTML: 我的HTML:

 <div class="card">
                <div class="card-header">Latest Print Jobs</div>

                <div class="card-body" style="padding:0px;">
                    <table class="table">
                        <thead>
                            <th>#</th>
                            <th>Employee Name</th>
                            <th>Job Number</th>
                            <th>Paper Size</th>
                            <th>Paper Type</th>
                            <th>Single or Doubled Sided</th>
                            <th>Quantity</th>
                            <th>Colour</th>
                            <th>Date</th>
                            <th>Edit</th>
                            <th>Print Cost</th>
                            <th>Paper Cost</th>
                            <th>Total Price</th>
                        </thead>
                        <tbody id="main-table">


                                      <tr>
                                       <td>14</td> 
                                        <td>Jafar</td> 
                                        <td>HCH_003</td> 
                                       <td>A3</td> 
                                        <td>Sirio Perla</td> 
                                         <td>Single</td> 
                                         <td>9</td> 
                                        <td>Black & White</td> 
                                         <td>2018-11-21 16:05:29</td> 
                                      <td> <a href="http://portal.test/prints/14" class="btn btn-default">Edit</a></td> 
                                      <td></td>
                                      <td></td>
                                      <td></td>
                                      </tr>

                                      <tr>
                                       <td>15</td> 
                                        <td>Jafar</td> 
                                        <td>HCH_099</td> 
                                       <td>A4</td> 
                                        <td>Sirio Perla</td> 
                                         <td>Single</td> 
                                         <td>9</td> 
                                        <td>Black & White</td> 
                                         <td>2018-11-21 16:20:22</td> 
                                      <td> <a href="http://portal.test/prints/15" class="btn btn-default">Edit</a></td> 
                                      <td></td>
                                      <td></td>
                                      <td></td>
                                      </tr>

                        </tbody>

                    </table>
                </div>
            </div>

My jQuery: 我的jQuery:

$(document).ready(function() {
  $("tr").each(function() {
    var paper_size = $("#main-table > tr > td:nth-child(4)").text();
    var paper_type = $("#main-table > tr > td:nth-child(5)").text();
    var single_or_double = $("#main-table > tr > td:nth-child(6)").text();
    var colour = $("#main-table > tr > td:nth-child(8)").text();
    var print_cost = $("#main-table > tr > td:nth-child(11)");
    var paper_cost = $("#main-table > tr > td:nth-child(12)");

    if (
      paper_type == "Sirio Perla" &&
      paper_size == "A4" &&
      colour == "Black & White"
    ) {
      $(print_cost).text("0.5");
      $(paper_cost).text("0.35");
    } else if (
      paper_type == "Sirio Perla" &&
      paper_size == "A4" &&
      colour == "Colour"
    ) {
      $(print_cost).text("2.5");
      $(paper_cost).text("0.35");
    } else if (
      paper_type == "Sirio Perla" &&
      paper_size == "A3" &&
      colour == "Black & White"
    ) {
      $(print_cost).text("0.1");
      $(paper_cost).text("0.6");
    } else if (
      paper_type == "Sirio Perla" &&
      paper_size == "A3" &&
      colour == "Colour"
    ) {
      $(print_cost).text("0.5");
      $(paper_cost).text("0.6");
    }
  });
});

my codepen to my problem is https://codepen.io/mrsalami/pen/QJQGjj 我的问题的codepen是https://codepen.io/mrsalami/pen/QJQGjj

You are missing few things 1st you need to understand the scope, you are iterating through tr you need to find the values inside it. 您缺少一些东西,首先需要了解范围,正在遍历tr时需要查找其中的值。 you do not need to go through the table again, you have $(this) as tr, find inside it and get the value. 您无需再次浏览该表,您可以将$(this)作为tr,在其中查找并获取值。

Color is at the 8th index not 7 颜色在第8个索引处而不是7个

Recommended approaches: 推荐方法:

  • You should do this calculations on server side 您应该在服务器端进行此计算
  • You should add class to each row and find the td using that classes td.className like this 您应该将类​​添加到每一行并使用td.className查找td

Here is the updated code, 这是更新的代码,

$(document).ready(function() {
  $("tr").each(function() { 
    var paper_size = $(this).find("td:nth-child(4)").text();
    var paper_type = $(this).find("td:nth-child(5)").text();
    var single_or_double = $(this).find("td:nth-child(6)").text();
    var colour = $(this).find("td:nth-child(8)").text();
    var print_cost = $(this).find("td:nth-child(11)");
    var paper_cost = $(this).find("td:nth-child(12)");
    debugger;
    if (
      paper_type == "Sirio Perla" &&
      paper_size == "A4" &&
      colour == "Black & White"
    ) {
      $(print_cost).text("0.5");
      $(paper_cost).text("0.35");
    } else if (
      paper_type == "Sirio Perla" &&
      paper_size == "A4" &&
      colour == "Colour"
    ) {
      $(print_cost).text("2.5");
      $(paper_cost).text("0.35");
    } else if (
      paper_type == "Sirio Perla" &&
      paper_size == "A3" &&
      colour == "Black & White"
    ) {
      $(print_cost).text("0.1");
      $(paper_cost).text("0.6");
    } else if (
      paper_type == "Sirio Perla" &&
      paper_size == "A3" &&
      colour == "Colour"
    ) {
      $(print_cost).text("0.5");
      $(paper_cost).text("0.6");
    }
  });
});

Demo: 演示:

 $(document).ready(function() { $("tr").each(function() { var paper_size = $(this).find("td:nth-child(4)").text(); var paper_type = $(this).find("td:nth-child(5)").text(); var single_or_double = $(this).find("td:nth-child(6)").text(); var colour = $(this).find("td:nth-child(8)").text(); var print_cost = $(this).find("td:nth-child(11)"); var paper_cost = $(this).find("td:nth-child(12)"); debugger; if ( paper_type == "Sirio Perla" && paper_size == "A4" && colour == "Black & White" ) { $(print_cost).text("0.5"); $(paper_cost).text("0.35"); } else if ( paper_type == "Sirio Perla" && paper_size == "A4" && colour == "Colour" ) { $(print_cost).text("2.5"); $(paper_cost).text("0.35"); } else if ( paper_type == "Sirio Perla" && paper_size == "A3" && colour == "Black & White" ) { $(print_cost).text("0.1"); $(paper_cost).text("0.6"); } else if ( paper_type == "Sirio Perla" && paper_size == "A3" && colour == "Colour" ) { $(print_cost).text("0.5"); $(paper_cost).text("0.6"); } }); }); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="card"> <div class="card-header">Latest Print Jobs</div> <div class="card-body" style="padding:0px;"> <table class="table"> <thead> <th>#</th> <th>Employee Name</th> <th>Job Number</th> <th>Paper Size</th> <th>Paper Type</th> <th>Single or Doubled Sided</th> <th>Quantity</th> <th>Colour</th> <th>Date</th> <th>Edit</th> <th>Print Cost</th> <th>Paper Cost</th> <th>Total Price</th> </thead> <tbody id="main-table"> <tr> <td>14</td> <td>Jafar</td> <td>HCH_003</td> <td>A3</td> <td>Sirio Perla</td> <td>Single</td> <td>9</td> <td>Black & White</td> <td>2018-11-21 16:05:29</td> <td> <a href="http://portal.test/prints/14" class="btn btn-default">Edit</a></td> <td></td> <td></td> <td></td> </tr> <tr> <td>15</td> <td>Jafar</td> <td>HCH_099</td> <td>A4</td> <td>Sirio Perla</td> <td>Single</td> <td>9</td> <td>Black & White</td> <td>2018-11-21 16:20:22</td> <td> <a href="http://portal.test/prints/15" class="btn btn-default">Edit</a></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </div> </div> 

I have modified your code slightly. 我已经稍微修改了您的代码。 I have changed your $.each loop and variables. 我已经更改了$.each循环和变量。

 $(document).ready(function () { var table = document.getElementById("main-table"); $("#main-table tr").each(function (i, row) { var $row = $(row); var paper_size = $row.find("td:nth-child(4)").text(); var paper_type = $row.find("td:nth-child(5)").text(); var single_or_double = $row.find("td:nth-child(6)").text(); var colour = $row.find("td:nth-child(8)").text(); var print_cost = $row.find(`td:nth-child(11)`); var paper_cost = $row.find("td:nth-child(12)"); if ( paper_type == "Sirio Perla" && paper_size == "A4" && colour == "Black & White" ) { $(print_cost).text("0.5"); $(paper_cost).text("0.35"); } else if ( paper_type == "Sirio Perla" && paper_size == "A4" && colour == "Colour" ) { $(print_cost).text("2.5"); $(paper_cost).text("0.35"); } else if ( paper_type == "Sirio Perla" && paper_size == "A3" && colour == "Black & White" ) { $(print_cost).text("0.1"); $(paper_cost).text("0.6"); } else if ( paper_type == "Sirio Perla" && paper_size == "A3" && colour == "Colour" ) { $(print_cost).text("0.5"); $(paper_cost).text("0.6"); } }); }); 
 <!DOCTYPE html> <html lang="en"> <head> <title>SO</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" integrity="sha384-/rXc/GQVaYpyDdyxK+ecHPVYJSN9bmVFBvjA/9eOB+pb3F2w2N6fc5qB9Ew5yIns" crossorigin="anonymous"> </head> <body> <div class="card"> <div class="card-header">Latest Print Jobs</div> <div class="card-body" style="padding:0px;"> <table class="table"> <thead> <th>#</th> <th>Employee Name</th> <th>Job Number</th> <th>Paper Size</th> <th>Paper Type</th> <th>Single or Doubled Sided</th> <th>Quantity</th> <th>Colour</th> <th>Date</th> <th>Edit</th> <th>Print Cost</th> <th>Paper Cost</th> <th>Total Price</th> </thead> <tbody id="main-table"> <tr> <td>14</td> <td>Jafar</td> <td>HCH_003</td> <td>A3</td> <td>Sirio Perla</td> <td>Single</td> <td>9</td> <td>Black & White</td> <td>2018-11-21 16:05:29</td> <td> <a href="http://portal.test/prints/14" class="btn btn-default">Edit</a></td> <td></td> <td></td> <td></td> </tr> <tr> <td>15</td> <td>Jafar</td> <td>HCH_099</td> <td>A4</td> <td>Sirio Perla</td> <td>Single</td> <td>9</td> <td>Black & White</td> <td>2018-11-21 16:20:22</td> <td> <a href="http://portal.test/prints/15" class="btn btn-default">Edit</a></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="js/index.js"></script> </body> </html> 

You were selecting 您正在选择

$("#main-table > tr > td:nth-child()") $(“#main-table> tr> td:nth-​​child()”)

which was wrong it will select child of both td at once it should iterate with the current object to find out the specific td one by one. 这是错误的,它将立即选择两个td的子对象,并且应该与当前对象进行迭代以逐一找出特定的td。 It should be with this to get the current td. 应当以此来获得当前的td。 Like 喜欢

$(this).find("td:nth-child()") $(本).find( “TD:第n个孩子()”)

 $(document).ready(function(){ $("tr").each(function() { var paper_size = $(this).find("td:nth-child(4)").text(); var paper_type = $(this).find("td:nth-child(5)").text(); var single_or_double = $(this).find("td:nth-child(6)").text(); var colour = $(this).find("td:nth-child(8)").text(); var print_cost = $(this).find("td:nth-child(11)"); var paper_cost = $(this).find("td:nth-child(12)"); if (paper_type == "Sirio Perla" && paper_size == "A4" && colour == "Black & White") { $(print_cost).text('0.5'); $(paper_cost).text('0.35'); }else if (paper_type == "Sirio Perla" && paper_size == "A4" && colour == "Colour"){ $(print_cost).text('2.5'); $(paper_cost).text('0.35'); }else if (paper_type == "Sirio Perla" && paper_size == "A3" && colour == "Black & White"){ $(print_cost).text('0.1'); $(paper_cost).text('0.6'); }else if (paper_type == "Sirio Perla" && paper_size == "A3" && colour == "Colour"){ $(print_cost).text('0.5'); $(paper_cost).text('0.6'); } }); }); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="card"> <div class="card-header">Latest Print Jobs</div> <div class="card-body" style="padding:0px;"> <table class="table"> <thead> <th>#</th> <th>Employee Name</th> <th>Job Number</th> <th>Paper Size</th> <th>Paper Type</th> <th>Single or Doubled Sided</th> <th>Quantity</th> <th>Colour</th> <th>Date</th> <th>Edit</th> <th>Print Cost</th> <th>Paper Cost</th> <th>Total Price</th> </thead> <tbody id="main-table"> <tr> <td>14</td> <td>Jafar</td> <td>HCH_003</td> <td>A3</td> <td>Sirio Perla</td> <td>Single</td> <td>9</td> <td>Black & White</td> <td>2018-11-21 16:05:29</td> <td> <a href="http://portal.test/prints/14" class="btn btn-default">Edit</a></td> <td></td> <td></td> <td></td> </tr> <tr> <td>15</td> <td>Jafar</td> <td>HCH_099</td> <td>A4</td> <td>Sirio Perla</td> <td>Single</td> <td>9</td> <td>Black & White</td> <td>2018-11-21 16:20:22</td> <td> <a href="http://portal.test/prints/15" class="btn btn-default">Edit</a></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </div> </div> 

var paper_size = $("#main-table > tr > td:nth-child(4)").text(); //It can be get all the 4th column data and produce the output (A3A4).So if condition as false

var paper_size = $(this).find('td').eq(3).text();// the output is (A3)
var paper_type = $(this).find('td').eq(4).text();
var colour = $(this).find('td').eq(7).text();

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

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