6

我正在开发一个 jquery 日期选择器。我需要选择一个日期,但在从日历中选择日期之后,我需要将日期分成三个单独的下拉菜单。一个代表一天,一个代表月份,一个代表年份。这是我的 jquery 脚本。

  <script type="text/javascript">
    $(document).ready(function() {

   $(function() {
    $( "#fullDate" ).datepicker({
    onClose: function(dateText, inst) {
       $('#day').val( dateText.split('/')[2] );
       $('#month').val( dateText.split('/')[1] );
   $('#year').val( dateText.split('/')[0] );
    }
 });
});
}); 
</script> 

HTML

<div class="demo">

<p>Date: <input id="fullDate" type="text" style="display:block"></p>
day:<select name="day" id="day" style="width:100px">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>

 month:<select name="month" id="month" style="width:100px">
 <option>1</option>
 <option>2</option>
  <option>3</option>
 <option>4</option>
 <option>5</option>
 </select>

year:<select name="year" id="year" style="width:100px">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div><!-- End demo -->
4

2 回答 2

7

工作演示请点击这里:] http://jsfiddle.net/gvAbv/13/ http://jsfiddle.net/gvAbv/8/

在演示中单击文本框并选择日期和宾果游戏,您将自动填充下拉菜单。

要注意dateText.split('/')[2]的是年份而不是日期:) 如果您要切换代码工作的地方。

dateText.split('/')[2]:年份;dateText.split('/')[0]: 月 ; dateText.split('/')[0]: day rest demo 会帮助你说清楚。

该演示有额外的部分,但它会帮助你!

代码

$(function() {
    $("#fullDate").datepicker({
        onClose: function(dateText, inst) {
            $('#year').val(dateText.split('/')[2]);
            $('#month').val(dateText.split('/')[0]);
            $('#day').val(dateText.split('/')[1]);
        }
    });
});

图片点击的另一个演示:http: //jsfiddle.net/zwwY7/

代码

$(function() {
    $("#fullDate").datepicker({
         buttonImage: 'icon_star.jpg',
        buttonImageOnly: true,
        onClose: function(dateText, inst) {
            $('#year').val(dateText.split('/')[2]);
            $('#month').val(dateText.split('/')[0]);
            $('#day').val(dateText.split('/')[1]);
        }
    });
});

​

图片 在此处输入图像描述

在此处输入图像描述

于 2012-06-04T05:44:45.450 回答
1

使用 datepicker onselect方法获取你需要的日期值 :: onselect info

像:

onSelect: function(dateText, inst) {            
        var datePieces = dateText.split('/');
        var month = datePieces[0];
        var day = datePieces[1];
        var year = datePieces[2];
        //define select option values for
        //corresponding element
        $('select#month').val(month);
        $('select#day').val(day);
        $('select#year').val(year);

}

你的意思是这样的吗

于 2012-06-04T05:42:46.487 回答