1

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

<input name="caseDate" id="caseDate"  min="2000-01-01" value="" type="date"  class="caseDate_h" >

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);
4

3 回答 3

0

您必须将日期解析为“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);

这应该有效。

于 2013-08-26T10:00:55.813 回答
0

将您的代码包装在里面:

 $(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-08-26

于 2013-08-26T09:47:41.613 回答
0

你应该把你的代码$(document).on('pageinit', function {});。这个jQuery Mobile: document ready vs page events可能会有所帮助。

于 2013-08-26T09:56:54.313 回答