我有一个bootstrap datepicker控件,我在Razor视图中用于日期选择 定义就像这样
<div class="col-md-4">
@Html.TextBoxFor(model => model.Date, new { @class = "datepicker form-control" })
@Html.ValidationMessageFor(model => model.Date)
</div>
并在javascript中
$(document).ready(function () {
$("#Date").datepicker({
showOn: 'both',
altFormat: 'MM-dd-yy',
dateFormat: 'MM-dd-yy',
changeMonth: true,
changeYear: true,
yearRange: "2014:2050"
});
});
我有一个按钮控件,应该在所选日期添加一天,并使用新的日期参数将页面重定向到另一个网址
function loadForPreDay() {
alert("s");
var d = $("#Date").val();
alert(d);
d.setDate(d.getDate() - 1);
var nd = new Date(newdate);
alert(nd);
window.location.href = '/Attendance/Select?date=' + nd;
}
从
调用<div class="col-md-4">
@Html.ActionLink("Previous day", null, null, null,new {@class="btn", onclick="loadForPreDay()"});
</div>
但对我来说它不起作用。 此外,日期字段还包含时间信息(默认情况下,选择今天的日期值) 如何使其与前一天按钮一起使用
答案 0 :(得分:0)
将Javascript代码修改为
//获取查询字符串参数
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}
$(document).ready(function () {
var myDate = GetParameterValues('date');
if(myDate != null && myDate != '')
{
$("#Date").datepicker({
showOn: 'both',
altFormat: 'MM-dd-yy',
dateFormat: 'MM-dd-yy',
defaultDate:myDate,
changeMonth: true,
changeYear: true,
yearRange: "2014:2050"
});
}
else
{
$("#Date").datepicker({
showOn: 'both',
altFormat: 'MM-dd-yy',
dateFormat: 'MM-dd-yy',
changeMonth: true,
changeYear: true,
yearRange: "2014:2050"
});
}
});