1

我正在尝试在我的网站上实现日期选择,但我遇到了一个小问题:要让它出现,我必须单击文本字段,单击外部,然后再次单击内部以显示。我该如何解决?这是我的代码的一部分:

                 function datePicker(){                         
                $( "#from" ).datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    changeYear: true,
                    onClose: function( selectedDate ) {
                        $( "#to" ).datepicker( "option", "minDate", selectedDate );
                        $( "#to" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
                    }
                });
                $( "#to" ).datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    changeYear: true,
                    onClose: function( selectedDate ) {
                        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
                        $( "#from" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
                    }
                });
            }   
    <input type="text" class ="date" id= "from" name="from" onclick="datePicker();"/>
    <BR>
    <input type="text" class = "date" id= "to" name="to" onfocus="datePicker();"/>  

我很感激任何帮助

4

2 回答 2

0
$(document).ready(function(){
     datePicker();
});

在 document.ready 上调用 datePicker 函数并删除 onclick 然后尝试

于 2013-01-16T12:20:33.010 回答
0

问题如下:在函数中datePicker()实例化日期选择器,只有在那之后它才会知道打开它。

删除函数行和 onclick="" 看起来像这样:

      <script type="text/javascript">
           jQuery(document).ready(function($){
                $( "#from" ).datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    changeYear: true,
                    onClose: function( selectedDate ) {
                        $( "#to" ).datepicker( "option", "minDate", selectedDate );
                        $( "#to" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
                    }
                });
                $( "#to" ).datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    changeYear: true,
                    onClose: function( selectedDate ) {
                        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
                        $( "#from" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
                    }
                });
            });  
       </script>
    <input type="text" class ="date" id= "from" name="from" />
    <BR>
    <input type="text" class = "date" id= "to" name="to"/>   
于 2013-01-16T12:20:59.840 回答