Javascript 抓取每个tr值并使用td值创建一个对象

Javascript 抓取每个tr值并使用td值创建一个对象,javascript,jquery,Javascript,Jquery,请检查下面的代码。在#btnConfirm按钮上,单击我必须抓取每个class=“invTr”data productid=“1”,然后在每个tr中,我必须抓取两个td文本,即-数量和单位价格,然后在var Items={}中存储整个对象。在jquery代码注释中,您将获得我想要的对象输出示例。任何人都可以从我当前的tr?我已经尝试过的代码类似于jquery bellow,但我仍然停留在这里,错误是push of undefined。有没有这样的想法?任何人都可以提供JSON对象输出,这也很好。

请检查下面的代码。在
#btnConfirm
按钮上,单击我必须抓取每个
class=“invTr”data productid=“1”
,然后在每个
tr
中,我必须抓取两个
td
文本,即-
数量和
单位价格
,然后在
var Items={}
中存储整个对象。在jquery代码注释中,您将获得我想要的对象输出示例。任何人都可以从我当前的
tr
?我已经尝试过的代码类似于jquery bellow,但我仍然停留在这里,错误是
push of undefined
。有没有这样的想法?任何人都可以提供JSON对象输出,这也很好。提前谢谢

当前我收到的错误是:

无法读取未定义的属性“push”

这是我的快速尝试

HTML:


invTrClassesObj.ProductId
给出了
未定义的
推送,因为
invTrClassesObj.ProductId
尚未设置

我添加了代码以获得所需的JSON对象输出

//单击操作时的确认按钮
$('#btnConfirm')。在('click',函数(){
var x=document.querySelectorAll(“.invTr”);
//控制台日志(x);
变量项={};
var invTrClassesObj=[];
var_invTrClasses=$('.invTr');
$。每个(_invTrClasses,function(i,invTrClasse){
invTrClassesObj.push(
{
“ProductId”:invTrClasse.getAttribute('data-ProductId'),
'Price':$(this.find(“td”).eq(2.html(),
'Quantiry':$(this.closest('tr')。find(.qty”).val()
});
}); 
console.log(invTrClassesObj);
//log(Items);//我想要这样的输出对象->{ProductId:1{Price:10.00,Quantiry:1},ProductId:2{Price:20.00,Quantiry:3}
});

标题
量
单价
总价
行动
拉文达事物10.00 20.55页
拉文达事物20.00 20.55页

确认
Hi,看起来
invTrClassesObj
没有键值
ProductId
。好的,但是检查jquery注释,我在final中给出了我的对象输出。您可以这样输出吗?另外,您的
invTrClasse.attr
部分不正确,因为
attr()
jQuery
的一部分,而您的invTrClasses是标准的
javascript
(html输出)。因此,您还需要执行
$(invTrClasse).attr
<table class="table table-hover table-sm">
    <thead>
        <tr>
            <th scope="col">Title</th>
            <th scope="col">Quantity</th>
            <th scope="col">Price Per Unit</th>
            <th scope="col">Total Price</th>
            <th scope="col">Action</th>
        </tr>
    </thead>
    <tbody>
    <tr class="invTr" data-productid="1"> <td>lavenda things</td> <td> <input type="number" class="form-control qty" style="width:75px;" pattern="[0-9]" min="1" max="10000" value="1"> </td> <td> 10.00 </td> <td> 20.55 </td> <td>pp</td> 
    </tr>
    <tr class="invTr" data-productid="2"> <td>lavenda things</td> <td> <input type="number" class="form-control qty" style="width:75px;" pattern="[0-9]" min="1" max="10000" value="3"> </td> <td> 20.00 </td> <td> 20.55 </td> <td>pp</td> 
    </tr>
    </tbody>
</table>
<button id="btnConfirm">Confirm</button>
//confirm button on click actions
$('#btnConfirm').on('click', function () {



  var Items = {};
  var invTrClassesObj = {};
  var _invTrClasses = $('.invTr');
  $.each(_invTrClasses, function (i, invTrClasse) {
    invTrClassesObj.ProductId.push(invTrClasse.attr('data-productId'));
  }); 


  //console.log(Items); //I want output object like this-> { ProductId:1 { Price:10.00, Quantiry:1 }, ProductId:2 { Price:20.00, Quantiry:3 } }

});