JavaScript语法错误:缺少正式参数

时间:2017-04-28 12:29:43

标签: javascript jquery arrays json

任何人都知道为什么此错误消息可能出现在控制台中以供下面的代码使用?我点击了它所说的问题的链接,然后它拉出了这一行:“$('tr')。each(function(){”。

(这可能比需要的代码多一些,但我不确定问题在哪里,所以我把它全部包括在内)

function buildData() {
        var data   = {
            "Id": 0,
            "CompanyId": companyId,
            "Date": $('#invoiceDate').val(),
            "Reference": $('#invoiceReference').val(),
            "Description": $('#additionalDetails').val(),
            "DeliveryAddress": {
                "Contact": $('#companyName').val(),
                "Address1": $('#address1').val(),
                "Address2": $('#address2').val(),
                "Address3": $('#address3').val(),
                "Address4": $('#address4').val(),
                "Address5": $('#address5').val(),
                "Postcode": $('#postcode').val()
            },
            "InvoiceAddress": {
                "Contact": $('#companyName').val(),
                "Address1": $('#address1').val(),
                "Address2": $('#address2').val(),
                "Address3": $('#address3').val(),
                "Address4": $('#address4').val(),
                "Address5": $('#address5').val(),
                "Postcode": $('#postcode').val()
            },
            "AllocatedToCredit": true,
            "Comments": "",
            "ContactId": $('#projectSelect').val(),
            "Items": [
                {
                    $('tr').each(function() {
                        "Id": 0,
                        "Number": 0,
                        "Type": 0,
                        "Description": $(this).find('.itemDetailsOutput').val(),
                        "NominalId": $(this).find('.nominalCodeId').val(),
                        "ProductId": 0,
                        "Quantity": $(this).find('.quantityOutput').val(),
                        "UnitPrice": {
                            "BaseValue": $(this).find('.unitPriceOutput').val(),
                            "ForeignValue": 0,
                            "CurrencyId": 0,
                            "BaseFormatString": "",
                            "ForeignFormatString": ""
                        },
                        "TaxCodeId": $('.taxCodeId').val(),
                        "TaxRate": $('.taxRate').val(),
                        "Subtotal": {
                            "BaseValue": $(this).find('.subtotalOutput').val(),
                            "ForeignValue": 0,
                            "CurrencyId": 0,
                            "BaseFormatString": "",
                            "ForeignFormatString": ""
                        },
                        "Tax": {
                            "BaseValue": $(this).find('.taxTotalOutput').val(),
                            "ForeignValue": 0,
                            "CurrencyId": 0,
                            "BaseFormatString": "",
                            "ForeignFormatString": ""
                        },
                        "RechargeableExpenseId": 0,
                        "Comments": "",
                        "DiscountedByItemId": 0,
                        "DiscountedItemId": 0,
                        "AnalysisCodes": [
                            0
                        ],
                        "IndentLine": true,
                        "NominalName": $(this).find('.nominalCodeName').val(),
                        "TaxCodeName":  $(this).find('.taxCodeName').val(),
                        "TimesheetId": 0,
                        "RechargeableEmployeeExpenseId": 0,
                        "TaxOverridden": false,
                        "CisDeductionType": 0,
                        "TransactionId": 0,
                        "Reserved": 0
                    })
                }
            ],
            "InvoiceType": 0,
        }
    }

    function saveInvoice() {
        var data = buildData();
        JA.put("api/" + companyId + "/Invoicing", data, function(response) {
            console.log(response);
        })
    }

    $('#saveInvoice').click(function() {
        saveInvoice();
    })

});

2 个答案:

答案 0 :(得分:0)

您无法在对象中间放置each()个调用。对象的语法意味着密钥必须放在您尝试运行each()循环的位置,密钥必须是关键字或字符串文字。

要解决此问题,您可以先从tr元素构建一个数组,然后将其作为Items属性的值提供,如下所示:

var items = $('tr').map(function() {
  return {
    "Id": 0,
    "Number": 0,
    "Type": 0,
    "Description": $(this).find('.itemDetailsOutput').val(),
    "NominalId": $(this).find('.nominalCodeId').val(),
    "ProductId": 0,
    // other properties...
  }
}).get();

var data = {
  // other properties...

  "Items": items,

  // some more properties...
}

答案 1 :(得分:0)

{} - 意味着您定义" object" 它由对键组成:值。

你应该在每个之前添加密钥;)

{
  res: $('tr').each( ...
相关问题