级联的下拉菜单反复出现 [英] cascading drop downs repeatedly populated
问题描述
我有一个三级层叠下拉列表
I have a three level cascading drop down
客户>项目>任务
当我选择一个客户时,我只想选择该客户的项目
When I pick a customer I just want to pick that customers projects
选择项目时,我只想选择项目任务
When I pick a project I just want to pick that projects tasks
我的项目>任务(第2到3级)运行正常
I have the project > task (level 2 to 3) working OK
但是,当我选择一个Customer时,它会填充Project OK,然后为每个项目重新填充Task.因此它会选择正确的任务,但是如果上面有3个项目,它将填充3次这些任务
But when I pick a Customer, it populates Project OK, but then re-populates Task for every project. So it picks the correct tasks but if there are 3 projects above, it will populate those tasks 3 times
因此,当我选择客户A时,它会正确填充项目1、2、3,并选择项目1作为默认项目
So when I pick Customer A, it correctly populates Project 1, 2, 3, and picks Project 1 as the default
但是假设项目1具有任务1和任务2,则任务下拉列表最终显示为:
But then assuming Project 1 has Task 1 and Task 2, the Task drop down ends up with:
任务1 任务2 任务1 任务2
Task 1 Task 2 Task 1 Task 2
我可以知道为什么会这样做,但是我不知道如何阻止它.我想我需要停止.append触发更改事件
I can see why it would be doing this but I don't how to stop it. I think I need to stop my .append from triggering the change event
即使那样也没有意义,因为无论如何应该清除它
Even then it doesn't make sense because it should be clearing it anyway
这是我的Javascript:
Here is my 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>'
);
})
}
});
}
推荐答案
refreshProjectTaskFromProject()是通过$ .each中的调用的.它必须在该循环之外.
refreshProjectTaskFromProject() is called from with in the $.each. It needs to be outside of that loop.
哦,您在append()之后缺少分号.
Oh, and you're missing a semi-colon after the append().
这篇关于级联的下拉菜单反复出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!