日期选择器在第二次点击时在 Ajax 调用后打开?如何在第一次点击时打开它


datepicker opens after ajax call on second click? how to open it on first click?

在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 调用后,我调用了相同的代码,但它在第一次单击时没有打开,但是它在第二次单击时打开?请帮我解决这个问题。

使用此 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" )
    });
});

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

<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>

尝试这样的事情:

$(document).ready(function(){
    $("#joindate").datepicker({
        // Options and methods here
    });
    $("#joindate").focus(function(){
          $("#joindate").datepicker("show");
    });
    $("#joindate").focus();
});
如果您想在日期

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