KendoUI DateTimePicker undefined on document.ready

元气小坏坏 提交于 2020-01-11 06:19:07

问题


I have a Kendo DateTimePicker control in an mvc (asp.net) partial view that I am trying to access from document.ready():

@(Html.Kendo().DateTimePickerFor(vvm => vvm.StartTime)
                    .Name("dtpVisitStart")
                    .Format("yyyy/MM/dd HH:mm tt")
                    .TimeFormat("HH:mm:tt")
                    .Events(e => e.Change("dtpVisitStart_Change")
                                )                               
)

The javascript:

$(document).ready(function () { 
    TestDTP();
});

function TestDTP() {
    var dtp = $("#dtpVisitStart").getKendoDateTimePicker();
    debugger;
}

When the debugger line runs dtp is undefined. How can I initialize this date time picker when the partial view loads?


回答1:


Your original approach should work if you put the ready block at the bottom of the page, or at least below the widget initialization code (also see this section of the docs).

If you put it above the @(Html.Kendo() section, TestDTP will run before the widget is initialized, since the widget initialization code is also wrapped in a jQuery ready block (and the various ready blocks are executed sequentially).




回答2:


I usually use this to get the instance:

var dtp = $('#dtpVisitStart').data('kendoDateTimePicker');




回答3:


What I ended up doing is initializing the datetimepicker from pure javascript and ditched the razor version:

function TestDTP() {
    $("#dtpVisitStart").kendoDateTimePicker({
        format: "MM/dd/yyyy HH:mm tt",
        timeFormat: "HH:mm", 
        change: dtpVisitStart_Change, 
        value: "@(startTime)"
    });
    var dtp = $("#dtpVisitStart").getKendoDateTimePicker();
    debugger;
}


来源:https://stackoverflow.com/questions/22722528/kendoui-datetimepicker-undefined-on-document-ready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!