如何在选择日期时自动选择日期
本文关键字:日期 选择 | 更新日期: 2023-09-27 18:28:55
我有两个类似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字段中,所以请有人帮忙找到解决方案
尝试
$('#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()]);
});
参考小提琴