从td单元获取数据时在数组中获取↵

时间:2019-05-09 13:20:21

标签: javascript node.js ajax ejs

当试图在对象的同一行中选中复选框以将其传递到其他页面时,我试图从td单元中获取数据,但我得到/ n作为回报。

productOrder.ejs

<table id="review-products">
  <thead>
    <tr>
      <th ></th>
      <th>Product Name</th>
      <th>Price </th>
      <th>Quantity</th>
    </tr>
  </thead>
  <tbody>
    <% products.forEach((products) => { %>
    <tr id="order-item-510" class="tst-orderItemRow">
      <td>
          <input type="checkbox"  value="<%= products.productId %>"  id="check" >
        </td>
      <td class="name" >
          <%= products.productName %>
      </td>
      <td class="currency" >
           <%= products.price %>
      </td>
      <td class="quant">
          <input type="text" class="foo" value="" >
      </td>
    </tr>
    <% }) %>
  </tbody>
</table>
    <button  id="save" name="button">Save</button> 

我的Ajax代码:

$("#review-products input[type=checkbox]:checked").each(function() {
  var row = $(this).closest("tr");
  var message0 = row.find('#check').val();
  var message1 = row.find('.name').text();
  var message2 = row.find('.currency').text();
  var message3 = row.find(".foo").val();
  var result = {
    check: message0,
    name: message1,
    p: message2,
    q: message3,
    total: message2 * message3
  }
  console.log(result);
});

在控制台中,我得到了:{check: "1", name: "↵ ball↵ ", p: "↵ 10↵ ", q: "3", total: 30}

1 个答案:

答案 0 :(得分:0)

trim您的价值

$("#review-products input[type=checkbox]:checked").each(function() {
  let row = $(this).closest("tr");
  let message0 = row.find('#check').val();
  let message1 = row.find('.name').text().trim();
  let message2 = row.find('.currency').text().trim();
  let message3 = row.find(".foo").val().trim();
  let result = {
    check: message0,
    name: message1,
    p: message2,
    q: message3,
    total: message2 * message3
  }
  console.log(result);
});