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

时间:2013-08-26 09:43:44

标签: jquery jquery-mobile

我想设置日期。我使用.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);

3 个答案:

答案 0 :(得分: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年8月26日

答案 1 :(得分:0)

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

答案 2 :(得分: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);

这应该有用。