我的页面中有一个 ComboBox,当客户写下任何字母时,我想将按键事件绑定(bind)到我的 Kendo ComboBox。
据我了解,剑道在 ComboBox 上没有任何按键事件。
我发现剑道有这样的东西来绑定(bind)值和函数:
kendo.data.binders.slide = kendo.data.Binder.extend({
refresh: function () {
var value = this.bindings["slide"].get();
if (value) {
$(this.element).slideDown();
} else {
$(this.element).slideUp();
}
}
});
来源:Click Here
但问题是我无法解决这个问题并使其触发 KendoComboBox 控件中 InputBox 上的按键事件。
请记住,我正在使用 MVVM,我不想使用像 $('k-input').keypress(...); 这样的东西。我想通过操作 kendo 为我们提供的 extend 方法在我的 kendo 框架中添加一些东西。
先感谢您。
最佳答案
这个比我想象的要复杂,但是您可以通过制作自定义 MVVM 绑定(bind)器来附加到 keyPress
来处理这个问题。 input
的事件元素,像这样:
kendo.data.binders.widget.keyPress = kendo.data.Binder.extend({
init: function (element, bindings, options) {
kendo.data.Binder.fn.init.call(this, element, bindings, options);
var binding = this.bindings.keyPress;
$(element.input).bind("keypress", function(){binding.get();});
},
refresh: function () {}
});
您可以将其绑定(bind)到 View 模型上的函数。
<input data-role="combobox"
data-text-field="text"
data-value-field="value"
data-bind="keyPress: onKeyPress, source: data"></input>
var viewModel = kendo.observable({
data: [
{text: "One", value: 1},
{text: "Two", value: 2}
],
onKeyPress: function () {
$("#output").append("<div>keyPress</div>");
}
});
这是 working jsFiddle .
关于mvvm - Kendo MVVM 和绑定(bind)或扩展自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14546606/