0
我试图使用jQuery Ajax将类别添加到下拉列表中。当选择一个选项时,我想加载子类别。
我面临的问题是,通过Ajax函数向下拉列表中添加选项似乎也触发了更改事件。我怎样才能避免这种情况或更好地重写我的代码,以避免这种行为?
这里是我的代码:
categoryHelper.loadLevel1 = function() {
// The control that needs to be filled with categories
var $control = $("#select1");
// This function runs $.getJSON() and fills the drop down list
// with option elements
fillDropDown(url, null, $control);
// When I select a value I want to run the function loadLevel2 which
// takes the currently selected value from level 1 as parameter
$control.change(categoryHelper.loadLevel2($control.val()));
};
代码为fillDropDown:
function fillDropDown(url, parameters, dropdown) {
/// all data has been loaded</param>
$.getJSON(url, parameters, function (data) {
$(dropdown).empty();
$(dropdown).append(createOption("", ""));
$(data).each(function() {
$(dropdown).append(createOption(this.value, this.text));
});
});
}
所有帮助表示赞赏!
谢谢,解决了这个问题。 :-) – Aetherix 2012-04-18 13:46:41