2013-12-17 62 views
2

我使用jQuery的1.10如何在文本框中設置默認日期? jQuery的

視圖模板

<script> 
    $(function() { 
    $("#departing_on").datepicker({ 
     dateFormat: "dd-mm-yy", 
     numberOfMonths:[1,2], 
     minDate: new Date() 
    }); 
    $("#return_on").datepicker({ 
     dateFormat: "dd-mm-yy", 
     numberOfMonths:[1,2], 
     minDate: new Date() 
    }); 
    }); 
</script> 

第一次同時選擇我想要默認今天的文本框中輸入日期。怎麼樣 ?

+0

OK ................. – Reigel

+0

你得到它'的setDate:新的Date()' – MaVRoSCy

+0

它不工作.. –

回答

3
$("#departing_on").val($.datepicker.formatDate("dd-mm-yy", new Date())); 
$("#return_on").val($.datepicker.formatDate("dd-mm-yy", new Date())); 

添加上述代碼在腳本的末尾。這是必需的,因爲datepicker插件沒有規定在初始化時在控件中設置日期。

+0

辛苦運氣它不工作... –

+0

這裏是一個工作示例http://jsfiddle.net/y9Ezw/ – Vishal

+0

哦!對不起,它只是正確的,我剛剛給新的腳本標籤,這就是爲什麼它不工作。感謝您的迴應... –

1

只是把在輸入字段;)

<input type="text" id="departing_on" value="23-03-2013" /> 
+1

感謝喬治,但這不是今天的日期... –

+0

哈哈,這是正確的,這只是一個隨機日期;)但你可以插入任何你想要的日期,例如拿破侖波拿巴的出生日期:p – giorgio

+0

告訴我你的出生日期我會放在那裏 –

2

不太清楚你的要求。你可以在JavaScript中獲取當前日期:

var now = new Date(); 

如果你想去jQuery中的DatePicker當前日期,就可以用它在任何像這樣的文本字段:

$('input.youTextFieldClass').datepicker({ dateFormat: 'mm-dd-yy', 
            changeMonth: true, 
            changeYear: true, 
            yearRange: '-70:+10', 
            constrainInput: false, 
            duration: '', 
            gotoCurrent: true}); 
+0

var now = new Date(); #Tue Dec 17 2013 18:17:47 GMT + 0530(IST)我想要17-12-2013格式。 –

+0

第一次選擇我想在文本框中顯示默認今天日期的日期。就是這樣 –

0

的解決方案是:

<input type="text" id="departing_on" class="datepicker form-control input-sm" value='' /> 

<script type="text/javascript"> 
function getCurrentDate(){ 
    today_date = new Date(); 

    today_Date_Str = ((today_date.getDate() < 10) ? "0" : "") + String(today_date.getDate()) + "-" +((today_date.getMonth() < 9) ? "0" : "") + String(today_date.getMonth() + 1)+ "-" +today_date.getFullYear(); 

    return today_Date_Str; 
} 
</script> 

<script> 
    $(function() { 
    $("#departing_on").datepicker({ 
     dateFormat: "dd-mm-yy", 
     numberOfMonths:[1,2], 
     minDate: new Date(), 
     // gotoCurrent: true 
    }); 
    $("#return_on").datepicker({ 
     dateFormat: "dd-mm-yy", 
     numberOfMonths:[1,2], 
     minDate: new Date() 
    }); 
    }); 
$(function() { 
    var date = new Date(); // replace with your date 
    $("#departing_on").val(getCurrentDate()); 
}); 
</script> 
相關問題