问题是数据没有正确地传递给回调函数。在构造函数中,将数据作为对象文字传递给回调函数。但是,当调用回调函数时,数据将作为字符串传递。这是因为对象文字在传递给回调函数时被转换为字符串。要解决此问题,需要将数据作为引用传递给回调函数。一种方法是使用箭头函数语法。以下是修复的代码:
class allEars {
â
constructor() {
â
$('#selectObj').on('change', (ev) =>
{
var cfg = this;
var retVal = null;
for (const prop in cfg.optList)
if (cfg.optList[prop].optID === $(this).val())
retVal = cfg.optList[prop].txt;
});
â
$('#tgtObj').val(retVal).trigger("change", { p:'param' });
}
â
async init()
{
this.optList = {
777: { optID:'1' ,txt:"first option" },
123: { optID:'2' ,txt:"second option" },
969: { optID:'n' ,txt:"option n" }
};
};
}