首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表输入值返回带有JQuery的NaN

表输入值返回带有JQuery的NaN
EN

Stack Overflow用户
提问于 2020-05-12 23:13:07
回答 4查看 86关注 0票数 0

我试图返回quantity类的值,但一直得到NaN。我可以访问价格类,但不能访问数量。不同之处在于数量有一个输入。

代码语言:javascript
复制
 <body>
   <tr>
     <td class="item pr-5">Sample</td>
     <td class="price pr-5">25.33</td>
     <td class="quantity pr-5"><input type="number" value="1" /></td>
     <td class="subtotal pr-5">$25.33</td>
     <td>
       <button class="btn btn-danger btn-sm remove">
         Remove Item
       </button>
     </td>
   </tr>
 </tbody>
代码语言:javascript
复制
$(document).ready(function () {
  $('tbody tr').each(function (i, ele) {
    var price = parseFloat($(ele).children('.price').text());
    console.log(price);
    var quantity = parseFloat($(ele).children('.quantity').text());
    console.log(quantity); // NaN
    var subtotal = price * quantity;
    // console.log(subtotal);
  });
});
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-05-12 23:20:11

在reach input标记中使用find()方法而不是使用两个children方法。并从input标记中获取值,使用val()而不是text()

var quantity = parseFloat($(ele).find('.quantity input').val());

代码语言:javascript
复制
$(document).ready(function() {
  $('tbody tr').each(function(i, ele) {
    var price = parseFloat($(ele).children('.price').text());
    console.log(price);
    var quantity = parseFloat($(ele).find('.quantity input').val());
    console.log(quantity); // NaN
    var subtotal = price * quantity;
    // console.log(subtotal);
  });
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tbody>
    <tr>
      <td class="item pr-5">Sample</td>
      <td class="price pr-5">25.33</td>
      <td class="quantity pr-5"><input type="number" value="1" /></td>
      <td class="subtotal pr-5">$25.33</td>
      <td>
        <button class="btn btn-danger btn-sm remove">
         Remove Item
       </button>
      </td>

  </tbody>
</table>

票数 0
EN

Stack Overflow用户

发布于 2020-05-12 23:16:47

这是因为<td class="quantity pr-5"><input type="number" value="1" /></td>这一行。此行没有text,但它有另一个子项

代码语言:javascript
复制
$(document).ready(function() {
  $('tbody tr').each(function(i, ele) {
    var price = parseFloat($(ele).children('.price').text());
    console.log(price);
    var quantity = parseFloat($(ele).children('.quantity').text());
    console.log(quantity); // NaN
    var subtotal = price * quantity;
    // console.log(subtotal);
  });
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <table>
    <tr>
      <td class="item pr-5">Sample</td>
      <td class="price pr-5">25.33</td>
      <td class="quantity pr-5"><input type="number" value="1" /></td>
      <td class="subtotal pr-5">$25.33</td>
      <td>
        <button class="btn btn-danger btn-sm remove">
         Remove Item
       </button>
      </td>

  </table>

票数 0
EN

Stack Overflow用户

发布于 2020-05-12 23:26:52

谢谢,我能够解决这个问题:

代码语言:javascript
复制
var quantity = parseFloat($(ele).children('.quantity').children().val());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61755307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档