最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to select date on click from Bootstrap Datepicker - Stack Overflow

programmeradmin5浏览0评论

I have Bootstrap Datepicker and I want to set default date when click on Set Date button.

HTML

<div class="input-append date datepicker no-padding" data-date-format="dd-mm-yyyy">
    <input class="input-medium" id="date" size="16" type="text"><span class="add-on"><i class="icon-th"></i></span>
</div>
<a href="#" id="set_date">Set Date</a>

jQuery

$(document).ready(function() {
    $('.datepicker').datepicker();
    
    $("#set_date").click(function(){
      date ="07/01/2015";
      $("#date").val(date); 
    });
});

I set date using jQuery but it do not show as selected date. My question is How to set date on click and show as selected date.

JS Fiddle

I have Bootstrap Datepicker and I want to set default date when click on Set Date button.

HTML

<div class="input-append date datepicker no-padding" data-date-format="dd-mm-yyyy">
    <input class="input-medium" id="date" size="16" type="text"><span class="add-on"><i class="icon-th"></i></span>
</div>
<a href="#" id="set_date">Set Date</a>

jQuery

$(document).ready(function() {
    $('.datepicker').datepicker();
    
    $("#set_date").click(function(){
      date ="07/01/2015";
      $("#date").val(date); 
    });
});

I set date using jQuery but it do not show as selected date. My question is How to set date on click and show as selected date.

JS Fiddle

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 16, 2015 at 13:06 SadikhasanSadikhasan 18.6k23 gold badges83 silver badges126 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

read this http://api.jqueryui./datepicker/#method-setDate

here after update your example http://jsfiddle/tuG6C/614/

$(document).ready(function() {
    $('.datepicker').datepicker();

    $("#set_date").click(function(){
      date ="07-01-2015";
      //$("#date").val(date); 
      $( '.datepicker' ).datepicker( "setDate", date );
    });
});

Update your javascript with

$(document).ready(function() {
    $('.datepicker').datepicker();

    $("#set_date").click(function(){
      date ="07/01/2015";

        var d = new Date();
        var curr_day = d.getDate();
        var curr_month = d.getMonth() + 1; //Months are zero based
        var curr_year = d.getFullYear();

        var today = curr_month + "/" + curr_day + "/" + curr_year;
        $("#date").val(today); 


    });
});

check js fiddle

http://jsfiddle/swapnilmotewar/tuG6C/609/

Please replace:

$("#date").val(date); 

With:

$(".datepicker").datepicker("update", date);
发布评论

评论列表(0)

  1. 暂无评论