0

我的页面中有两个文本框,分别保存日期和日期。我有一些按钮。当我单击 1day 按钮时,开始日期和结束日期应设置为今天的日期。当我单击 1 周按钮时,to date 应设置为今天的日期,from 日期应设置为今天前 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>
4

2 回答 2

1

使用日期对象

例如:

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

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

进一步阅读:

格式相关:

于 2013-09-21T07:16:20.407 回答
1

试试这个代码:

看看这个演示 -

演示 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));     
       }
  });
于 2013-09-21T07:23:37.930 回答