我有一个datepicker
,我想在页面加载时显示当前日期,但是当我使用简单的输入字段进行操作时,它正在工作,但是datepicker
输入字段未使用值< / p>
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12"; //January is 0!
var mm = month[today.getMonth()];
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;
console.log(today)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
<link href="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<label for="deliveryDate" id="commonHeader"> Delivery Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
现在我要在变量today
中获取今天的日期,现在我想在页面加载时将该值存储在datepicker输入字段中。
答案 0 :(得分:1)
您可以使用.val()
:
设置页面加载量:
$(function() {
$('#deliveryDate').datepicker({
dateFormat: 'dd/mm/yyyy'
});
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12"; //January is 0!
var mm = month[today.getMonth()];
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;
$('#deliveryDate').val(today);
console.log(today);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
<link href="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<button id="btn">Set DATE</button>
<br>
<br>
<label for="deliveryDate" id="commonHeader"> Delivery Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
设置点击值:
$(function() {
$('#deliveryDate').datepicker({
dateFormat: 'dd/mm/yyyy'
});
$('#btn').on('click', function() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12"; //January is 0!
var mm = month[today.getMonth()];
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;
$('#deliveryDate').val(today);
console.log(today);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
<link href="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<button id="btn">Set DATE</button>
<br>
<br>
<label for="deliveryDate" id="commonHeader"> Delivery Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
答案 1 :(得分:0)
设置日期选择器时,您需要使用defaultDate
属性。
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
defaultDate: '01/01/1970'
});
您也可以在其中使用日期对象,或者今天使用0。
今天:
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
defaultDate: 0
});
* Api Reference Link: http://api.jqueryui.com/datepicker/#option-defaultDate
答案 2 :(得分:0)
Content-Type: multipart/form-data
$(document).ready(function() {
var todayDate = (function(){
var d = new Date();
var day = d.getDate();
day = day > 9 ? day : '0' + day ;
var month = (d.getMonth() + 1);
month = month > 9 ? month : '0' + month;
var _value = day + '/' + month + '/' + d.getFullYear();
return _value;
})();
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
value: todayDate
});
});