admin管理员组文章数量:1434972
On my page, I have a form that opens up in modal. It has a couple of fields and date is one of them. I want to set it as default to today's date. My modal can be seen below
<!--main modal-->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style ="background-color: #38BFEF;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><center><font color="white">New Food Delivery</font></center></h4>
</div>
<div class="modal-body">
<!---inside pop up content start-->
<form>
<center>
<p>
<label for="Delivery Number">Delivery Number:</label>
<input type="text" name="delivery_number" id="delNumber" style="width:150px;" autofocus="autofocus">
</p>
<p>
<label for="Delivery Date">Delivery Date:</label>
<input type="date" name="datepicker" style="margin-left:4%;width:150px;" id="datepicker">
</p>
<p>
<label for="Supplier">Supplier:</label>
<input type="text" name="supplier" style="margin-left:10%;width:150px;" id="supplier">
</p>
<p>
<label for="Supplier">Cost of Delivery:</label>
<input type="text" name="cost" style="margin-left:1%;width:150px;" id="cost">
</p>
</center>
<center><input type="button" id="submit" class="btn btn-info btn-lg vbtn" value="Submit" onclick="submitDelivery()" data-toggle="modal" data-target="#confirmModal"></center>
</form>
<!---inside pop up content end-->
</div>
<div class="modal-footer" style ="background-color: #38BFEF;">
<input type="button" id="close" class="btn btn-default" data-dismiss="modal" value="Cancel">
</div>
</div>
</div>
</div>
Then I have a couple of javascript functions for different things, for example going through the inputs with enter presses, submitting the form when enter is pressed etc.
One of the functions is to set the date for the #datepicker and it looks like this
$('#myModal').on('shown.bs.modal', function (e) {
$('#delNumber').focus();
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (day)+"/"+(month)+"/"+ now.getFullYear();
//alert(today);
$('#datePicker').val(today);
})
However, when I open the modal in Chrome, the box still says dd/mm/yyyy, seems like the function doesn't work properly?
Another issue is that when I open the above code in Firefox, it doesn't seem to recognize the Date box as date input type.
On my page, I have a form that opens up in modal. It has a couple of fields and date is one of them. I want to set it as default to today's date. My modal can be seen below
<!--main modal-->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style ="background-color: #38BFEF;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><center><font color="white">New Food Delivery</font></center></h4>
</div>
<div class="modal-body">
<!---inside pop up content start-->
<form>
<center>
<p>
<label for="Delivery Number">Delivery Number:</label>
<input type="text" name="delivery_number" id="delNumber" style="width:150px;" autofocus="autofocus">
</p>
<p>
<label for="Delivery Date">Delivery Date:</label>
<input type="date" name="datepicker" style="margin-left:4%;width:150px;" id="datepicker">
</p>
<p>
<label for="Supplier">Supplier:</label>
<input type="text" name="supplier" style="margin-left:10%;width:150px;" id="supplier">
</p>
<p>
<label for="Supplier">Cost of Delivery:</label>
<input type="text" name="cost" style="margin-left:1%;width:150px;" id="cost">
</p>
</center>
<center><input type="button" id="submit" class="btn btn-info btn-lg vbtn" value="Submit" onclick="submitDelivery()" data-toggle="modal" data-target="#confirmModal"></center>
</form>
<!---inside pop up content end-->
</div>
<div class="modal-footer" style ="background-color: #38BFEF;">
<input type="button" id="close" class="btn btn-default" data-dismiss="modal" value="Cancel">
</div>
</div>
</div>
</div>
Then I have a couple of javascript functions for different things, for example going through the inputs with enter presses, submitting the form when enter is pressed etc.
One of the functions is to set the date for the #datepicker and it looks like this
$('#myModal').on('shown.bs.modal', function (e) {
$('#delNumber').focus();
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (day)+"/"+(month)+"/"+ now.getFullYear();
//alert(today);
$('#datePicker').val(today);
})
However, when I open the modal in Chrome, the box still says dd/mm/yyyy, seems like the function doesn't work properly?
Another issue is that when I open the above code in Firefox, it doesn't seem to recognize the Date box as date input type.
Share Improve this question asked Jan 13, 2017 at 14:30 ArtleMaksArtleMaks 1713 silver badges19 bronze badges2 Answers
Reset to default 3In form you have id as datepicker and you are trying to access datePicker (Notice the capital P)using jquery.
Instead of using DD/MM/YYYY
you need to use format YYYY-MM-DD
So resulting string will be:
var today = (now.getFullYear() + '-' + month + '-' + day);
$('#datepicker').val(today);
Here is working example: https://jsfiddle/panamaprophet/2Lt2q3ga/
本文标签: javascriptSetting current date for a date input type box in a modalStack Overflow
版权声明:本文标题:javascript - Setting current date for a date input type box in a modal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745645296a2668098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论