在使用添加行下拉菜单添加行时,在最近添加的行中未绑定

时间:2019-01-30 08:00:33

标签: javascript jquery model-view-controller

我正在尝试通过api绑定下拉列表,而用户添加行绑定了该下拉列表,但未在用户添加的第一行上显示,而在另一行下拉列表中也显示了绑定下拉列表项,但未选择特定值项目

$(document).ready(function () {
        var counter = 0;

        $("#addrow").on("click", function () {
            var newRow = $("<tr>");
            var cols = "";

            cols += '<td> <input type="text" class="form-control" name="Date' + counter + '"/></td>';
            cols += '<td><select class="ddlAccountCategory form-control p-0" onchange="BindItem()" id="ddl" name="AccountTitle' + counter + '">' + Titles + '</select></td>';
            cols += '<td><input type="text" class="form-control" name="AccountCategory' + counter + '"/></td>';
            cols += '<td><input type="text" class="form-control" name="Debit' + counter + '"/></td>';
            cols += '<td><input type="text" class="form-control" name="Credit' + counter + '"/></td>';

            cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger "  value="Delete"></td>';
            newRow.append(cols);
            $("table.order-list").append(newRow);
            counter++;

            $(function () {
                var ddlCustomers = $("#ddl");
                //var obj = { EmployeeName: EmployeeName };
                //JSON.stringify(obj);
                ddlCustomers.empty().append('<option selected="selected" value="0" disabled = "disabled">Loading.....</option>');
                $.ajax({
                    headers: {
                        'Authorization': 'Basic '
                            + sessionStorage.getItem("token")
                    },
                    type: "GET",
                    url: 'http://localhost:62982/' + "api/AccountCategory/GetAllAccountCategory",


                    data: '{}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        ddlCustomers.empty().append('<option selected="selected" value="0">Please Select ..</option>');
                        $.each(response, function () {
                            //JSON.stringify(response);
                            $("#ddl").html('');
                            var options = '';
                            //options += '<option value="' + 0 + '">' + 'Select Country' + '</option>';
                            options += '<option value="' + 0 + '">' + 'Select Account Category' + '</option>';
                            for (i = 0; i < response.length; i++) {
                                Titles = options += '<option value="' + response[i].AccountCategoryID + '">' + response[i].Title + '</option>';

                            }
                            $('AccountTitle' + counter + '').append(options);

                            //ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
                        });
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });

并且此代码用于绑定项目

function BindItem() {
        var AccountCategoryID = $('#ddl option:selected').attr("value");
        //var CountryID = $("#ddCountry").val();
        // var SessionClassSectionID = $("#ddlSessionClassSection").val();
        //if (SessionID == null || SessionID == "" || SessionID == undefined || ClassSectionRelationID == null || ClassSectionRelationID == "" || ClassSectionRelationID == undefined)
        if (AccountCategoryID == null || AccountCategoryID == "" || AccountCategoryID == 0) {

            swal({
                title: "Select Category First !",
                text: "Kindly select Category first for getting Asset type !",
                type: "error",
                showConfirmButton: true
            });

            //    $("#ddCountry").val("");
        }
        else {
            var id = $('#ddl option:selected').attr("value");
            var AccountCategoryID = Number(id);
            $.ajax({
                headers: {
                    'Authorization': 'Basic '
                        + sessionStorage.getItem("token")
                },
                url: '@URLs.BASE_INFRASTRUCTURE' + "api/AccountCategory/ GetReferenceByAccountID",
                data: { "AccountCategory": AccountCategoryID },
                type: "GET",
                dataType: 'json',
                crossDomain: true,
                contentType: "application/json;charset=utf-8",
                success: function (response) {
                    //var da=response[0].Template;
                    da = response[0].ReferenceID;
                    $("#txtReference").val(response[0].ReferenceID);
                    //  State.empty().append('<option selected="selected" value="0">Please select</option>');

                }


            });
        }
    }

用户在文本字段中选择与项目显示相关的下拉参考

0 个答案:

没有答案