简体   繁体   English

如何将JavaScript绑定到在MVC5视图中创建的表内的复选框和datetimepicker

[英]How to bind javascript to checkboxes and datetimepicker inside table created in MVC5 view

Basically, 基本上,

I have a table created in MVC view. 我在MVC视图中创建了一个表。

Each row has a checkbox and a datetime HTML input control associated with the part order. 每行都有一个复选框和一个与零件订单关联的日期时间HTML输入控件。 I'd like to automatically set datetime picker to today's date if checkbox is selected. 如果选择了复选框,我想将日期时间选择器自动设置为今天的日期。 I know how to do it if my checkbox and datetime have a predefined IDs (see jquery script below), but in this case, I don't know ahead of time how many rows I will have ahead of time. 如果我的复选框和日期时间具有预定义的ID(请参阅下面的jquery脚本),我知道该怎么做,但是在这种情况下,我不提前知道将有多少行。 I'm somewhat stuck on trying to figure an easiest way to setup unique IDs and subsequently read them from the javascript. 我有些想尝试一种最简单的方法来设置唯一ID,然后从javascript中读取它们。

Here is the snippet of my code from the MVC View: 这是我在MVC视图中的代码片段:

<tbody>
@foreach (var partOrder in Model.PartOrders)
 {
  <tr>
   <td>@Html.DisplayFor(m => partOrder.Part.Description, new { htmlAttributes = new { @class = "form-control" } })</td>
   <td class="text-center">@Html.EditorFor(m => partOrder.IsPartReceived, new {htmlAttributes = new { @class = "checkbox isPartReceived" } })</td>
   <td class="text-center">@Html.DisplayFor(m => partOrder.OrderedOn, new { htmlAttributes = new { @class = "form-control" } })</td>
   <td>@Html.EditorFor(m => partOrder.ReceivedOn, new { htmlAttributes = new { @class = "form-control partReceivedOn"} })</td>
 </tr>
}
</tbody>

The first @Html.EditorFor is generating the checkbox, and the second one is the datetime picker. 第一个@ Html.EditorFor生成复选框,第二个是日期时间选择器。

Here is the example of jquery that I use elsewhere on the page to do something similar: 这是我在页面其他地方使用jquery进行类似操作的示例:

            $("#IsBoxRequested").click(function () {
                var checkBox = $("#IsBoxRequested");
                var timeBox = $("#BoxRequestedOn");
                setTime(checkBox, timeBox);
            });


    function setTime(checkbox, timebox) {
        if (checkbox.prop('checked') == true) {
            timebox.val(getToday());
        }
        else {
            timebox.val(null);
        }
    }
    function getToday() {
        var now = new Date();
        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        return today = now.getFullYear() + "-" + (month) + "-" + (day);
    }

Any tips on how I can this trick on dynamically created checkbox inputs are greatly appreciated. 非常感谢您提供有关如何动态创建复选框输入的此技巧的任何提示。

Many thanks again to @StephenMuecke I have solved my issue. 再次感谢@StephenMuecke,我已经解决了我的问题。

First, I switched to for loop as shown below: 首先,我切换到for循环,如下所示:

            <tbody>
                @for (int i = 0; i < Model.PartOrders.Count(); i++)
                {
                    @Html.HiddenFor(m=>m.OrderedPartIDs[i])
                    <tr>
                        <td>@Html.DisplayFor(m => m.PartOrders[i].Part.Description, new { htmlAttributes = new { @class = "form-control" } })</td>
                        <td class="text-center"><label style="display:block">@Html.EditorFor(m => m.PartOrders[i].IsPartReceived, new { htmlAttributes = new { @class = "checkbox isPartReceived" } })</label></td>
                        <td class="text-center">@Html.DisplayFor(m => m.PartOrders[i].OrderedOn, new { htmlAttributes = new { @class = "form-control" } })</td>
                        <td>@Html.EditorFor(m => m.PartOrders[i].ReceivedOn, new { htmlAttributes = new { @class = "form-control partReceivedOn" } })</td>
                    </tr>
                }
            </tbody>

Second, I've updated javascript to simple: 其次,我将javascript更新为简单的:

            $(".isPartReceived").click(function () {
                var timeBox = $(this).closest('tr').find('.partReceivedOn');
                setTime($(this), timeBox);

            });

Everything works like a charm now! 一切现在都像魅力一样!

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

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