我正在尝试使用jquery日期选择器中选择的日期来测试填充标签。我在标签上得到的是[OBJECT] [OBJECT]。有任何想法吗?
以下是代码:
@using (Html.BeginForm("Calculate Days", "Calendar", FormMethod.Post))
{
<h2>Enter The Number Of Days To Display</h2>
@Html.TextBoxFor(model => model.DaysToBeViewed)
<label id="dateSelected"></label>
<input type="submit" value="Submit" onclick="daysAdded()" />
}
<script type="text/javascript">
var dateSelected = $("#date").datepicker("getDate");
function daysAdded(addDaysEntered, dateSelected) {
var oneDAY = 1000 * 60 * 60 * 24
var addDaysEnteredMiliSec = addDaysEntered.getTime()
var DateMiliSec = Date.getTime()
var datesAddedmiliSec = Math.abs(addDaysEnteredMiliSec + DateMiliSec)
return Math.round(datesAddedmiliSec / oneDay)
}
</script>
<script>
document.getElementById('dateSelected').innerHTML = dateSelected;
</script>
<script>
$(function () {
$('#Date').datepicker();
});
</script>
答案 0 :(得分:0)
如http://api.jqueryui.com/datepicker/#method-getDate所述,getDate()方法返回datepicker的当前日期,如果没有选择日期,则返回null。返回的对象类型是Date对象,而不是字符串。
您需要将Date对象转换为字符串。例如:
var dateSelected = $("#date").datepicker("getDate").toString();
http://www.w3schools.com/jsref/jsref_obj_date.asp涵盖了可用的日期对象方法。