datePicker仅在双击时显示

时间:2013-01-16 12:15:32

标签: javascript php jquery

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

                 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();"/>  

我感谢任何帮助

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

问题如下:在函数datePicker()中,你实例化了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"/>