如何将日期选择器默认为占位符?

时间:2015-01-27 21:32:00

标签: javascript jquery kendo-ui kendo-asp.net-mvc kendo-datepicker

以下是我的日期选择器的代码:

<div class="editor-field">
                    @(Html.Kendo().DatePickerFor(model => model.Date)
                        .HtmlAttributes(ViewBag.Disabled ? (object)new { @class = "formDatePicker", disabled = "disabled", @id = "Date", style = "width:150px", placeholder = "Select Date" } : new { @class = "formDatePicker", @id = "Date", style = "width:150px", placeholder = "Select Date" })
                        .Events(e => e.Open(@<text>function(e){BrowserCompatibility.DisableDatePickerYearsOnSomeBrowsers(e);}</text>))
                        .Format(@HCulture.GetShortDateFormat())
                        .Events(e => e.Change(@<text>function(e){
                            $("#DateString").val($('#Date').val());
                            checkInputValid();
                            }</text>))
                    )
                </div>

我试过这个解决方案,但它不起作用:

 var datePicker = $("#Date").data('kendoDatePicker');
 datePicker.input.val(input.attr('placeholder'));

1 个答案:

答案 0 :(得分:5)

最简单的方法是:

$("#Date").attr("placeholder", "type date");

但您也可以这样做:

var datePicker = $("#Date").data('kendoDatePicker');
datePicker.element.attr("placeholder", "type date");

请注意element中的DatePicker引用原始input元素。

检查以下代码段中的两种方法。

$(document).ready(function() {
  $("#Date").kendoDatePicker({
  });

  $("#change1").on("click", function() {
    $("#Date").attr("placeholder", "type date 1");
  });
  $("#change2").on("click", function() {
    var datePicker = $("#Date").data('kendoDatePicker');
    datePicker.element.attr("placeholder", "type date 2");
  });
});
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.web.min.js"></script>

<button class="k-button" id="change1">Change Method 1</button>
<button class="k-button" id="change2">Change Method 2</button>
<input id="Date" disabled="disabled"/>