如何在type =“date”.in jquery中设置日期

我想设置日期。我使用.val()函数,但它在android中不起作用。

 

JS:

  var now = new Date(); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear()+"-"+(month)+"-"+(day) ; var now = new Date(); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear()+"-"+(month)+"-"+(day) ; $('#caseDate').val(today); $("#caseDate").attr("value", today); 

将代码包装在内:

  $(document).ready(function(){ var now = new Date(); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear()+"-"+(month)+"-"+(day) ; var now = new Date(); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear()+"-"+(month)+"-"+(day) ; $('#caseDate').val(today); $("#caseDate").attr("value", today); }); 

可能输出

2013年8月26日

你应该把你的代码放在$(document).on('pageinit', function {}); 。 这个jQuery Mobile:文档就绪vs页面事件可能会有所帮助。

您必须将日期解析为“yyyy-mm-dd”的格式

 var todaysDate = new Date(); var month = parseInt(todaysDate.getMonth() / 10) <= 0 ? "0" + todaysDate.getMonth() : todaysDate.getMonth(); var date = parseInt(todaysDate.getDate() / 10) <= 0 ? "0" + todaysDate.getDate() : todaysDate.getDate(); console.log(month); console.log(date) var today = todaysDate.getFullYear() + "-" + month + "-" + date; console.log(today); $('#caseDate').val(today); 

这应该工作。