简体   繁体   English

如何通过JQuery选择器构建对象列表并在Ajax中发送

[英]How to build a list of objects from JQuery selectors and send in Ajax post

1. Explanation of my screen and fields: 1.我的屏幕和字段说明:

Considering each value on the screen as editable inputs with the names: name_{id of the row} 将屏幕上的每个值都视为具有以下名称的可编辑输入: name_{id of the row}

ordinary_1 = 4.5
ordinary_2 = 3.8
overtimeApproved = false
overtimeApproved = true
overtime15_1 = 0
overtime15_2 = 0
overtime20_1 = 0
overtime20_2 = 0

Table on the screen: 屏幕上的表: 在此处输入图片说明

When I click on Approve button , I will need to build a json with the values when " Overtime Approved is checked ", as below: 当我单击“ 批准”按钮时 ,我将需要使用“ 检查超时”批准时的值构建一个json,如下所示:

{
  "Id": "2",
  "Ordinary": "3.8",
  "OvertimeApproved": "true",
  "Overtime15": "0"
  "Overtime20": "0"
}

To build this Json I need to select the fields using JQuery Selectors and then send the Json in an Ajax command to my MVC in order to update those fields. 要构建此Json,我需要使用JQuery选择器选择字段,然后将Ason的Json发送给我的MVC以更新这些字段。

I don't know how to use editable datatables, and the way I explained might be the quickest solution. 我不知道如何使用可编辑的数据表,而我的解释方式可能是最快的解决方案。

So I need: 所以我需要:

  1. Build that Json using JQuery Selector, each() command, etc. 使用JQuery Selector,each()命令等构建Json。

  2. Send that in an Ajax (This I know how to do) 用Ajax发送(我知道该怎么做)

var data = [];
$("#tableID tr").each(function() {
    var cols = $(this).find("td");
    data.push({
        Id: cols.eq(0).prop('name').split('_')[1],
        Ordinary: cols.eq(0).text(),
        OvertimeApproved: cols.eq(1).find(":checkbox").is(":checked") ? "true" : "false",
        Overtime15: cols.eq(2).text(),
        Overtime20: cols.eq(3).text()
    });
});

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

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