从javascript添加knockoutjs模板事件,而不是将事件添加到模板本身

Add knockoutjs template event from javascript, as opposed to adding to event to template itself

本文关键字:事件 添加 javascript knockoutjs      更新时间:2023-09-26

我有以下敲除js模板:

<select id="ddlExpertise" data-bind="options: expertiseList(), optionsCaption: 'All', value: expertiseField" ></select>

有没有办法将"event:{change:doFilter}"从JavaScript/淘汰库添加到模板中?

我一开始不想把它放在模板中。

如果我理解正确,您希望对select中的更改做出反应,并调用doFilter方法。实现这一点的正确KO"方式"是订阅expertiseField上的更改,如下所示:

this.expertiseField.subscribe(function (newValue) { 
    doFilter(newValue); 
});

每次有人在select KO上选择新值时,都会更新experiseField,并调用您的订阅函数。它接收新的值,允许您过滤某些内容或其他内容。KO内部使用更改事件来进行双向数据绑定。

希望这能有所帮助。