[英]How to bind a (basic) Kendo DropDownList made from a select element, with data in option elements, to a javascript object using Kendo MVVM?
I can't seem to work out, or see examples any where in the doco or on the web for how to do this. 我似乎无法解决问题,也无法在doco或网络上的任何位置查看示例以了解如何执行此操作。
I have a simple select element that I have marked up to be a Kendo DropDownList. 我有一个简单的select元素,已将其标记为Kendo DropDownList。
HTML: HTML:
<div id="View">
<select
id="DropDownList"
data-role="dropdownlist"
data-value-primitive="true"
data-bind="value: OptionID">
<option value="1">Option One</option>
<option value="2">Option Two</option>
<option value="3">Option Three</option>
</select>
</div>
JavaScript: JavaScript的:
$(document).ready(function() {
var oDataSource = {
OptionID: -1
};
// MVVM Bind View to Record
kendo.bind($('#View'), oDataSource);
// Log OptionID set in oDataSource by DropDownListing Binding
$('#DropDownList').data('kendoDropDownList').bind('select', function (e) {
console.log('Selected Option: ' + oDataSource.OptionID);
});
});
The console.log of the oDataSource.OptionID shows -1 every time. oDataSource.OptionID的console.log每次显示-1。
I have created a fiddle here that demonstrates the code above: http://jsfiddle.net/codeowl/CDrFS/3/ 我在这里创建了一个小提琴,用于演示上面的代码: http : //jsfiddle.net/codeowl/CDrFS/3/
What am I doing wrong? 我究竟做错了什么?
Regards, 问候,
Scott 斯科特
Two questions: 两个问题:
oDataSource
as an ObservableObject
otherwise your variable is not bound to DropDownList
. 您应该将oDataSource
定义为ObservableObject
否则您的变量未绑定到DropDownList
。 change
handler instead since select
is triggered when the option is selected but the input
is not changed yet. 您需要使用change
处理程序,因为在select
选项但input
尚未更改时会触发select
。 So your code should be: 因此,您的代码应为:
var oDataSource = kendo.observable({
OptionID: -1
});
// MVVM Bind View to Record
kendo.bind($('#View'), oDataSource);
// Log OptionID set in oDataSource by DropDownListing Binding
$('#DropDownList').data('kendoDropDownList').bind('change', function (e) {
console.log('Selected Option: ' + oDataSource.OptionID);
});
See you JSFiddle modified here : http://jsfiddle.net/OnaBai/CDrFS/4/ 看到您在这里修改过的JSFiddle: http : //jsfiddle.net/OnaBai/CDrFS/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.