2013-09-21 16 views
0

我有兩個文本框,這些文本框從日期到目前爲止都處於頁面中。我有一些按鈕。當我點擊按鈕1day時,應該將開始日期和日期設置爲今天的日期。當我點擊按鈕1周時,至今應該設置爲今天的日期,從今天開始的日期應該設置爲1周。其他按鈕具有類似的功能。我該如何使用jQuery或JavaScript來做到這一點?請爲我提供一個FIDDLE在點擊文本框中設置日期

  <div id="date"> 
       From: <input type="text" size="6" name="date" id="from" class="tcal"> 
       To: <input type="text" size="6" name="date1" id="to" class="tcal"> 
       <button id="1day"> 1 Day </button> 
       <button id="month"> 1 Month</button> 
       <button id="3month"> 3 Month</button> 
       <button id="year"> 1 year</button> 
      </div> 
+0

[你到目前爲止試過了什麼](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – Unknown

+0

所以你要求爲你寫代碼? –

+0

看我編輯的小提琴。 –

回答

1

試試這個代碼:

看看這個演示 -

DEMO jsfiddle

$('button').click(function(){ 
     id = $(this).attr('id'); 
     $('#to').val((new Date()).getMonth() + "-" + (new Date()).getDate() + "-" + (new Date()).getFullYear()); 

     if(id == '1day'){ 
      $('#from').val((new Date()).getMonth() + "-" + ((new Date()).getDate()-1) + "-" + (new Date()).getFullYear());   
     } 
     if(id == 'month'){ 
      $('#from').val(((new Date()).getMonth()-1) + "-" + (new Date()).getDate() + "-" + (new Date()).getFullYear());  
     } 

     if(id == '3month'){ 
      $('#from').val(((new Date()).getMonth()-3) + "-" + (new Date()).getDate() + "-" + (new Date()).getFullYear());  
     } 

     if(id == 'year'){ 
      $('#from').val((new Date()).getMonth() + "-" + (new Date()).getDate() + "-" + ((new Date()).getFullYear()-1));  
     } 
    }); 
1

使用日期對象

對於前:

$("#1day").click(function() { 
$("#from").val(new Date().toString()); 
$("#to").val(new Date().toString()) ; 
}); 

http://jsfiddle.net/sureshbm13/DWUrh/7/

移動進一步閱讀:

相關格式:

+0

如何將格式更改爲dd-mm-yyyy? –

+0

非常感謝參考:-) –

+1

@AnushaHoney所以你最終得到的代碼:)與項目的好運氣。 –

相關問題