2015-12-06 88 views
1

好,所以我已經看到Bootstrap Datepicker : Changing start date to tomorrow,我認爲會讓我的權利,但它沒有。defaultViewDate不工作引導日期選擇器

$("#datepickerStart, #datepickerEnd").datepicker({ 
 
    todayHighlight: true, 
 
    endDate: '+367d' 
 
}) 
 

 
var fullDate = new Date(); 
 
    console.log(fullDate); 
 
var twoDigitMonth = fullDate.getMonth() + ""; 
 
if (twoDigitMonth.length == 1) 
 
    twoDigitMonth = "0" + twoDigitMonth; 
 
var twoDigitDate = fullDate.getDate() + ""; 
 
if (twoDigitDate.length == 1) 
 
    twoDigitDate = "0" + twoDigitDate; 
 
var $currentDate = twoDigitMonth + "/" + twoDigitDate + "/" + fullDate.getFullYear(); 
 
var $endYear = fullDate.getFullYear() + 1; 
 
    console.log($currentDate); 
 
    console.log($endYear); 
 
var $endDate = twoDigitMonth + "/" + twoDigitDate + "/" + $endYear; 
 
    console.log($currentDate); 
 
    console.log($endDate); 
 

 
$("#datepickerStart").find("input").attr("value", $currentDate); 
 

 
$("#datepickerEnd").find("input").attr("value", $endDate); 
 

 
$("#datepickerEnd").datepicker({ 
 
    defaultViewDate: { 
 
    year: $endYear, 
 
    month: twoDigitMonth, 
 
    day: twoDigitDate 
 
    }, 
 
    format: 'mm/dd/yyyy', 
 
})
@import url('https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'); 
 
@import url('https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker3.css');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script> 
 
<div class="form-group col-sm-3 colFlushL hideme" id="endDateWrapper"> 
 
    <label class="control-label" for="datepickerEnd">End <small><span class="text-muted">(max 1 year)</span></small> 
 
    </label> 
 

 
    <div class="input-group date colFlush" data-provide="datepicker" id="datepickerEnd"> 
 
    <input type="text" class="form-control" name="datepickerEnd" value=""> 
 
    <span class="input-group-addon"><i 
 
      class="glyphicon glyphicon-th"></i></span> 
 
    </div> 
 
</div>

不漂亮,但它的工作原理 - 除了爲最終選擇器默認視圖(值正常工作)(即在輸入字段中的值是罰款和367天的限制工作。

不知是否是因爲iddata-provide連接到input-groupinput,但你必須這樣做,以使該圖標工作作爲日期選擇器的一部分,並沒有圖標的工作是從UX點愚蠢

有什麼想法?

PS我們正在談論http://bootstrap-datepicker.readthedocs.org/en/latest/options.html#id3

enter image description here

回答

0

似乎來自你輸入的是黑客...

這項工作and the icon too

$("#datepickerStart, #datepickerEnd").datepicker({ 
 
    todayHighlight: true, 
 
    endDate: '+367d' 
 
}) 
 

 
var fullDate = new Date(); 
 
    console.log(fullDate); 
 
var twoDigitMonth = fullDate.getMonth() + ""; 
 
if (twoDigitMonth.length == 1) 
 
    twoDigitMonth = "0" + twoDigitMonth; 
 
var twoDigitDate = fullDate.getDate() + ""; 
 
if (twoDigitDate.length == 1) 
 
    twoDigitDate = "0" + twoDigitDate; 
 
var currentDate = twoDigitMonth + "/" + twoDigitDate + "/" + fullDate.getFullYear(); 
 
var endYear = fullDate.getFullYear() + 1; 
 
    console.log(endYear+' '+twoDigitMonth+' '+twoDigitDate); 
 
var endDate = twoDigitMonth + "/" + twoDigitDate + "/" + endYear; 
 

 
$("#datepickerEnd").attr("value", endDate); 
 

 
$("#datepickerEnd").datepicker({ 
 
    defaultViewDate: { 
 
    year: endYear, 
 
    month: twoDigitMonth, 
 
    day: twoDigitDate 
 
    }, 
 
    format: 'mm/dd/yyyy', 
 
})
@import url('https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'); 
 
@import url('https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker3.css');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script> 
 

 
<div class="form-group col-xs-5 colFlushL hideme" id="endDateWrapper"> 
 
    <label class="control-label" for="datepickerEnd">End <small><span class="text-muted">(max 1 year)</span></small> 
 
    </label> 
 

 
    <div class="input-group date colFlush" data-provide="datepicker"> 
 
    <input id="datepickerEnd" type="text" class="form-control" name="datepickerEnd" value=""> 
 
    <div class="input-group-addon"><span class="glyphicon glyphicon-th"></span></div> 
 
    </div> 
 
    
 
</div>

+0

感謝gazillion。黑客是我試圖遵循Bootstrap 3. BS 2使用了''標籤,但BS 3希望我們使用''作爲插件(參見http://getbootstrap.com/components/#input-groups-basic),並且這個會導致很多問題。現在我將放棄文檔,因爲這會繼續出現,''標籤似乎正常工作。謝謝 – BeNice

+0

@OldMauiMan編輯:看一看,它也適用於'span' XD – Blag

+0

真的 - 我太累了,無法照顧。我會通過我的代碼整理日期選擇器(我有10個編程器)。再次感謝,但當我能指責Bootstrap時,我感到更開心:-)生活呃! (ps每次我在@Blag開始的時候吃它 - 對不起) – BeNice