简体   繁体   English

如何使用Kendo MVVM将选择元素制成的(基本)Kendo DropDownList与选项元素中的数据绑定到javascript对象?

[英]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: 两个问题:

  1. You should define oDataSource as an ObservableObject otherwise your variable is not bound to DropDownList . 您应该将oDataSource定义为ObservableObject否则您的变量未绑定到DropDownList
  2. You need to use 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.

相关问题 如何从Kendo MVVM数据绑定函数调用中获取实际的html元素 - How to get actual html element from Kendo MVVM data-bind function call 使用Kendo MVVM下拉列表时如何强制选择第一项 - How to force selecting first item when using Kendo MVVM Dropdownlist 如何启用禁用MVVM的Kendo DropDownList? - How to enable a MVVM-disabled Kendo DropDownList? 如何MVVM数据绑定剑道单选按钮值属性? - how to mvvm data-bind kendo radiobutton value attribute? 如何在不使用HTML包装器的情况下将视图模型数据绑定到Kendo DropDownlist - How to bind view model data to Kendo DropDownlist without using the html wrapper 如何使用JavaScript绑定Kendo网格单元 - How to bind the kendo grid cell using javascript 使用键盘存储以前的剑道下拉列表选项 - Store previous kendo dropdownlist option using keyboard 使用Selenium WebDriver(Java)从kendo-dropdownlist中选择一个值,仅在单击该元素时才在“检查元素”选项卡中看到该元素 - select a value from kendo-dropdownlist using Selenium WebDriver(Java) for which the element is seen in the Inspect Element tab only when clicked on it 在Javascript中禁用Kendo DropDownList - Disabled Kendo DropDownList in Javascript Kendo Dropdownlist使用JavaScript添加样式 - Kendo Dropdownlist adding style using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM