如何在选择日期时进行自动日期选择

时间:2014-11-04 04:49:38

标签: c# jquery asp.net-mvc asp.net-mvc-4 datepicker

我有两个像HolidayDate这样的字段,如下所示,

 [Required]
 [DataType(DataType.Date)] 
 [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
 [DisplayName(Constants.DisplayName.HolidayDate)]
  public virtual DateTime HolidayDate { get; set; }

 [StringLength(50)]
 [DisplayName(Constants.DisplayName.HolidayDay)]
 public virtual string HolidayDay { get; set; }

在我的cshtml文件中

<div class="col-md-3">
  <label for="" class="control-label">Date</label>
  <br />
  @Html.EditorFor(model => model.HolidayDate, new { @class = "form-control"})
    .DisableIf(() => Model.IsReadOnly == true)
</div>
<div class="col-md-3">
  <label for="" class="control-label">Day</label>
  <br />
  @Html.TextBoxFor(model => model.HolidayDay, new { @class = "form-control"})
    .DisableIf(() => Model.IsReadOnly == true)
</div>

如果我在HolidayDate字段中选择日期,则该日期的日期应自动在HolidayDay字段中指定,所以请任何人都可以帮助找到解决方案

1 个答案:

答案 0 :(得分:0)

尝试

$('#HolidayDate').change(function() {
  var date = new Date($(this).val());
  $('#HolidayDay').val(date.getDate());
});

或者如果您想显示星期几

var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
$('#HolidayDate').change(function () {
  var date = new Date($(this).val());
  $('#HolidayDay').val(daysOfWeek[d.getDay()]);
});

Refer fiddle