JSON在jquery中添加一个额外的行和空白数据

时间:2017-12-13 08:49:30

标签: javascript jquery json

我在JSON中为输入元素添加数据,但是如果我添加两行,则在json中添加三行。我不知道为什么。

以下是我的json

[{"VendorCode":"Vendor 1","FromDate":"1/1/1970","ToDate":"1/1/1970"},{"VendorCode":"Vendor 1","FromDate":"2/1/1970","ToDate":"1/1/1970"},{"VendorCode":"","FromDate":"","ToDate":""}]

它显示三行,但实际上它有两行。这是保存的代码。

 $(".vendorDaterow").each(function () {

        var ID = "";
        var Name = "";
        var ToDate = "";
        var FromDate = "";
        var NameValues = [];

        var allInputs = $(this).find("input");
        var objectToAdd = {};
        var Code = "";

            for (var i = 0; i < allInputs.length; i++) {
                if (allInputs[i].name == "nmVendorData") {
                    objectToAdd["VendorName"] = allInputs[i].value;
                    Name = allInputs[i].value;
                    NameValues = Name.split('/');
                    Code = NameValues[0];
                    Name = NameValues[0];

                }
                else if (allInputs[i].name == "spFromDate") {
                    objectToAdd["ToDate"] = allInputs[i].value;
                    FromDate = allInputs[i].value;
                }
                else if (allInputs[i].name == "spToDate") {
                    objectToAdd["FromDate"] = allInputs[i].value;
                    ToDate = allInputs[i].value;
                }
            }

        arrVendorValues.push({
            "VendorCode": Code,
            "FromDate": FromDate,
            "ToDate": ToDate
        });
    });

已更新

<tr id="vendorlisttr1">
                            <td>
                                <div class="row noPadding vendorForm">
                                    <div class="vendorDaterow">
                                        <div class="vendorName" id="dvVendorNameData1">
                                            <label>SP Vender Name</label><span><input type="text" value="" name="nmVendorData" id="txtVendorName1" /></span>
                                        </div>
                                        <div class="vendorFromDate">
                                            <label>From Date</label><span class="datepicker"><input type="text" value="" name="spFromDate" id="spFromDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                        </div>
                                        <div class="vendorToDate">
                                            <label>To Date</label><span class="datepicker"><input type="text" value="" name="spToDate" id="spToDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                        </div>
                                    </div>
                                    <div class="add">
                                        <i class="fa fa-plus" aria-hidden="true"></i>
                                    </div>

                                </div>
                            </td>
                        </tr>

2 个答案:

答案 0 :(得分:1)

可以使用 npm start 作为 class 三次。但是两个人有输入或 "vendorDaterow" 条件。因此,请在代码中查看以下内容。

check the not empty

感谢。

答案 1 :(得分:0)

我认为额外添加的行{"VendorCode":"","FromDate":"","ToDate":""}没有任何值。所以你可以试试这个,

if(Code && FromDate && ToDate) { // This you can change accordingly
  arrVendorValues.push({
        "VendorCode": Code,
        "FromDate": FromDate,
        "ToDate": ToDate
    });
}

您可以使用适当的HTML内容更新问题吗?理解额外的迭代可能会有所帮助。