层叠下降反复填充

时间:2016-06-25 09:05:25

标签: javascript ajax asp.net-mvc-5

我有一个三级级联下拉

客户>项目>任务

当我选择客户时,我只想选择客户项目

当我选择一个项目时,我只想选择项目任务

我有项目>任务(2级到3级)工作正常

但是当我选择一个客户时,它会填充Project OK,但随后会为每个项目重新填充Task。所以它选择了正确的任务,但如果上面有3个项目,它将填充这些任务3次

因此,当我选择客户A时,它会正确填充项目1,2,3,并选择项目1作为默认

但是假设项目1有任务1和任务2,任务下拉结束时:

任务1 任务2 任务1 任务2

我明白为什么会这样做,但我不知道如何阻止它。我想我需要阻止我的.append触发更改事件

即便如此,它也没有意义,因为无论如何它应该清除它

这是我的Javascript:

/*
    This java script is for CREATE and EDIT popup dialogs on the Task Index page
    - Manage cascading drop downs (Client > Project, Project > Task)
    - Synchronise check box with hidden field
*/



$(document).ready(function () {
    //When Customer is changed reload Project
    // Project function reloads project tasks
    $("#Customer_ID").change(
        function () {
            // refresh projects
            refreshProjectFromClient();
        }
        )

    // When project is changed reload task
    $("#CustomerProject_ID").change(
        function () {
            refreshProjectTaskFromProject();
        }
        )

    // When chargeable is changed sync the hidden field
    // A change in a checkbox called MyCheckBox_CB 
    // will be reflected in a hidden field called MyCheckBox
    $(":checkbox").change(
        function () {
            $("#" + this.name.replace("_CB", "")).val((this.checked ? 1 : 0));
        }
        )
})





// This is called when the customer combo changes
// It reloads the project combo with the customers active projects
function refreshProjectFromClient() {
    // clear drop down
    $("#CustomerProject_ID").empty();

    // Get new project dataset via ajax based on customer and populate drop down
    $.ajax({
        type: 'POST',
        //url: '@Url.Action("GetProjects")',
        url: window.location.toString() + '/GetProjects',
        dataType: 'json',
        data: { CustomerID: $("#Customer_ID").val() },
        // success is called when dataset returns
        success: function (p) {
            // Populate with each returned member
            $.each(p, function (i, pr) {
                // is this append triggering the task onchange?
                $("#CustomerProject_ID").append(
                    '<option value="' + pr.Value + '">' +
                    pr.Text + '</option>'
                    )
                // now that it's loaded, load the project tasks
                refreshProjectTaskFromProject();
            })
        }
    });
}


function refreshProjectTaskFromProject() {
    $("#CustomerProjectTask_ID").empty();

    // Get new tasks dataset via ajax based on project and populate drop down
    $.ajax({
        type: 'POST',
        url: window.location.toString() + '/GetProjectTasks',
        dataType: 'json',
        data: { ProjectID: $("#CustomerProject_ID").val() },
        // success is called when dataset returns
        success: function (t) {
            // Populate with each returned member
            $.each(t, function (i, ta) {
                $("#CustomerProjectTask_ID").append(
                    '<option value="' + ta.Value + '">' +
                    ta.Text + '</option>'
                    );
            })
        }
    });
}

1 个答案:

答案 0 :(得分:0)

在$ .each中使用from调用refreshProjectTaskFromProject()。它需要在那个循环之外。

编辑:哦,你在追加()之后错过了一个分号。