简体   繁体   English

将文本框设置为 jquery datepicker

[英]Setting textbox to jquery datepicker

I've got a loop on my model (on the view page) for all drivers that was submitted by the user earlier in the application.对于用户之前在应用程序中提交的所有驱动程序,我的模型(在视图页面上)有一个循环。 For each driver additional information is required, thus a for loop was created.对于每个驱动程序都需要附加信息,因此创建了一个 for 循环。 In this loop 3 controls are created, which one of them are/should be a datepicker (datepicker is not showing).在这个循环中创建了 3 个控件,其中一个是/应该是日期选择器(未显示日期选择器)。

My problem is that because the control is created on the fly the id does not stay consistant thus setting the datepicker in js is not working.我的问题是,因为控件是动态创建的,所以 id 不会保持一致,因此在 js 中设置日期选择器不起作用。

This is what I've done:这就是我所做的:

    @{int driverSumCount = 1;}
    @for (int i = 0; i < Model.Count; i++)
    {
       <div class="form-group">
           <label class="col-lg-12 control-label summary-list">
               @driverSumCount<text>.</text>
               @Model[i].DriverDesc
           </label>
        </div>
        <hr />
        if (i == 0)
        { /@ Other controls are added here @/
            <div class="form-group">
                    <div class="col-lg-12 padding-none">
                         @Html.LabelFor(m => @Model[i].LicenceExpDate, new { @class = "col-lg-12 control-label label-input" })
                         <div class="col-lg-7 col-md-7 col-sm-7 col-xs-12">
                             <div class="input-group input-text" style="text-align:center; vertical-align:central; margin:0px !important; padding:0px !important; border:1px solid #9f9f9f !important; border-left:0px !important;">
                              @Html.TextBoxFor(m => @Model[i].LicenceExpDate, new { @class = "form-control datepicker", data_dob = "dd/mm/yyyy", placeholder = "DD/MM/YYYY" })
                              <span class="CalendarOuter">
                                  <span class="input-group-addon btn" style="background-color:#e8ecf0; border:1px solid #e8ecf0 !important; height:97% !important; padding:1px !important; margin:1px !important;">
                                      <span class="fa datepicker" style="color:#405E7C; background-color:#e8ecf0; padding-top:13px;">
                                         <img src="~/Images/png/CalendarN.png" height="27" width="27" />
                                       </span>
                                 </span>
                           </span>
                       </div>
                  </div>
                  <div class="col-lg-7 col-md-7 col-sm-7 col-xs-12">
                       @Html.ValidationMessageFor(m => @Model[i].LicenceExpDate)
                  </div>
             </div>
        </div>
       }
       else
       {
           //more code
       }
   }

Normally I would call do this :通常我会调用这样做:

   $("#LicenceExpDate").datepicker({
    beforeShow: function(input, inst) {
        setTimeout(function() {
            inst.dpDiv.css({
                top: $("#LicenceExpDate").offset().top + 55
            });
        }, 0);
    },
    //other settings
});
$(".datepicker").on("click", function (e) {
    $("#LicenceExpDate").datepicker("show");
});

How would I accomplish a date time picker to pop up?我将如何完成一个日期时间选择器弹出? Currently nothing is happening when I click on the textbox as well as the glyphicon/image...目前,当我单击文本框以及字形/图像时什么也没有发生...

The control looks like this (I typed in the date):控件看起来像这样(我输入了日期):

在此处输入图片说明


Solution解决方案

    <div class="form-group">
                <div class="col-lg-12 padding-none">
                    @Html.LabelFor(m => @Model[i].LicenceExpDate, new { @class = "col-lg-12 control-label label-input" })
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-12">
                        <div class="input-group input-textGlyphicon" style="background-color:#e8ecf0 !important; padding-right:4px !important;">
                            @Html.TextBoxFor(m => @Model[i].LicenceExpDate, new { @class = "form-control input-textGlyphicon datepicker", data_dob = "dd/mm/yyyy", placeholder = "DD/MM/YYYY" })
                            <div class="input-group-addon" style="width:60px;background-color:#e8ecf0 !important;">
                                <span class="fa datepicker-trigger" style="background-color:#e8ecf0">
                                    <img src="~/Images/png/CalendarN.png" height="27" width="27" />
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-12">
                        @Html.ValidationMessageFor(m => @Model[i].LicenceExpDate)
                    </div>
                </div>
            </div>

And my js now looks like this:我的 js 现在看起来像这样:

    var serverDate = $("#ServerDate").val();
var dateStart = moment($("#ServerDate").val(), "DD/MM/YYYY").format('YYYY/MM/DD');

$('.datepicker').each(function () {
    $(this).datepicker({
            beforeShow: function (input, inst) {
                setTimeout(function () {
                    inst.dpDiv.css({
                        top: $(".datepicker-triggerN").offset().top + 55
                    });
                }, 0);
            },
            minDate: new Date(dateStart),
            dateFormat: "dd/mm/yy",
            dayNamesMin: ["S", "M", "T", "W", "T", "F", "S"]});
});

$(".datepicker-trigger").on("click", function (e) {
    var $datepicker = $(this).closest('.form-group').find('.datepicker');
    $datepicker.datepicker("show");
})

Have you tried instantiating multiple instances via CSS class selector?您是否尝试过通过 CSS 类选择器实例化多个实例? Also, your span.datepicker tags that should just act as triggers are also set up to be picked up by the CSS class selector, so you need to change that to something like span.datepicker-trigger .此外,您的span.datepicker标记应该只用作触发器也设置为由 CSS 类选择器选取,因此您需要将其更改为类似span.datepicker-trigger

script脚本

$('.datepicker').each(function(){
    $(this).datepicker({ /* add callbacks here */ });
});

$(".datepicker-trigger").on("click", function (e) {
    var $datepicker = $(this).closest('.form-group').find('.datepicker');
    $datepicker.datepicker("show");
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM