在第二次点击后调用ajax后,datepicker会打开吗?如何在第一次点击时打开它?

时间:2012-07-31 13:08:12

标签: php

在ajax调用之前

我写日期选择器的代码是:

<script type="text/javascript">
    $(function() {
        $("#birthdate").focus(function() {
            $(this).datepicker().datepicker( "show" )
        });
    });
    $(function() {
        $("#joindate").focus(function() {
            $(this).datepicker().datepicker( "show" )
        });
    });
    </script>           

在onfocus事件后调用此函数:

<tr>
              <td align="left">
                <input type="text" id="birthdate"  name="birthdate"  onfocus="$('#birthdate').datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-20y -1m -247d',yearRange: '-50:+0'});"   tabindex="14"  style="width:300px; "   value="<? if($get_emp!='0')echo $employee_birth; ?>" /><span style="color:#F00;">*</span></td>                                       

    <td>
        <input type="text" name="joindate" id="joindate" onfocus="$('#joindate').datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-3y -1m -247d',yearRange: '-50:+0'});"  tabindex="15" style="width:300px; " value="<? if($get_emp!='0')echo $employee_join; ?>"/><span style="color:#F00;">*</span>
        </td>
     </tr>
在ajax调用之后

我调用相同的代码,但是第一次点击时它不会打开,但是第二次点击会打开吗? 请帮我解决这个问题..........................

2 个答案:

答案 0 :(得分:0)

使用此javascript。您只需初始化datepicker()一次

$(function() {
    $('#birthdate').datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-20y -1m -247d',yearRange: '-50:+0'});
    $("#joindate").datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-3y -1m -247d',yearRange: '-50:+0'});
    $("#birthdate, #joindate").focus(function() {
        $(this).datepicker( "show" )
    });
});

我从输入标签中删除了focus属性:

<input type="text" id="birthdate"  name="birthdate" tabindex="14"  style="width:300px; "  value="<? if($get_emp!='0')echo $employee_birth; ?>" /><span style="color:#F00;">*</span></td>                                       

<input type="text" name="joindate" id="joindate" "  tabindex="15" style="width:300px; " value="<? if($get_emp!='0')echo $employee_join; ?>"/><span style="color:#F00;">*</span>

答案 1 :(得分:0)

尝试这样的事情:

$(document).ready(function(){
    $("#joindate").datepicker({
        // Options and methods here
    });
    $("#joindate").focus(function(){
          $("#joindate").datepicker("show");
    });
    $("#joindate").focus();
});

您还可以在“onSelect”事件上执行$ .post(),如果您想在日期的实际点击事件上发布日期,而不必将其绑定到表单提交。