gpt4 book ai didi

jquery - .trigger ("change") 无法在 .on ("change") 中工作

转载 作者:行者123 更新时间:2023-12-01 04:38:24 29 4
gpt4 key购买 nike

我使用了 .on("change") 事件函数,因为我的整个代码中有一部分是动态变化的。 .trigger("change").change() 中工作正常,但在 .on("change") 中不行。这是我的代码:

    $('#popupContent').on('change', '#gradeYearLevel', function() {
var $course = $('#course');
var $section = $('#section');

if ($(this).val() > 4) {
$type = 0;
}
else {
$type = 1;
}
if ($('#gradeYearLevel').val() == '') {
$course.empty().append($("<option>", {"value": ""}).html("-- Select --"));
$section.empty().append($("<option>", {"value": ""}).html("-- Select --"));
}
else {
$.getJSON(BASE_PATH + "getJSONCoursesByType/" + $type, function(data) {
$course.empty().append($("<option>", {"value": ""}).html("-- Select --"));
$.each(data, function(k,v){
$course.append(
$("<option>", {"value": v.id}).html(v.title)
);
});
});
$.getJSON(BASE_PATH + "getJSONSectionsByType/" + $type, function(data) {
$section.empty().append($("<option>", {"value": ""}).html("-- Select --"));
$.each(data, function(k,v){
$section.append(
$("<option>", {"value": v.id}).html(v.name)
);
});
});
}
}).trigger("change");

最佳答案

问题是因为您触发了从 on() 返回的 #popupContent 元素上的 change 事件,不是委托(delegate)事件处理程序正在监听的#gradeYearLevel

您需要在子元素上调用trigger(),以便事件可以冒泡。试试这个:

$('#popupContent').on('change', '#gradeYearLevel', function() {
// your code here...
})

$('#gradeYearLevel').trigger("change");

关于jquery - .trigger ("change") 无法在 .on ("change") 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44975420/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com