JavaScript 传递今天的日期 onchange 事件

JavaScript to pass todays date onchange event

我有一个带有下拉菜单的视图 (MVC5, C#)。当用户为订单选择新状态时,我想从下拉列表中更新 CurrentStatusDate EditFor 框。我非常接近,但还没有雪茄。这是我的

<script type="text/javascript">
$(function () {
    $("[name='CheckStatus']").change(function () {

        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is 0!
        var yyyy = today.getFullYear();

        if (dd < 10) {
            dd = '0' + dd;
        }

        if (mm < 10) {
            mm = '0' + mm;
        }

        today = mm + '/' + dd + '/' + yyyy;
        $("CurrentStatsDate").val($(today).val());      
  });
});

这是一行,我相信我正在努力获得 . 的正确语法。 . .

 $("CurrentStatsDate").val($(today).val());

我已经为 CurrentStatusDate 对象添加了我的视图代码

   <div class="form-group">
            @Html.LabelFor(model => model.CurrentStatusDate, (string)"Current Status Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CurrentStatusDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly",  @Value = ViewBag.ThisDay } })
                @Html.ValidationMessageFor(model => model.CurrentStatusDate, "", new { @class = "text-danger" })
            </div>
        </div>

您的选择器缺少 # 并且还有错字。

$("CurrentStatsDate") 应该是 $("#CurrentStatusDate")

此外,$(today).val() 将失败。 today 被设置为上一行的字符串。所以你可以只使用 $("#CurrentStatusDate").val(today)