Focus不适用于具有验证和掩码的Datepicker

时间:2013-12-16 16:23:26

标签: javascript jquery asp.net-mvc-4 datepicker

MVC4,IE11:附加了Datepicker的输入字段有2个。那些输入字段需要有日期掩码:99/99/9999。并在onClose事件上验证日期。如果日期无效,则需要清除输入字段并将焦点设置回日期。在输入字段中输入无效日期后,在字段的tab-out上:焦点未设置到该输入字段中,并且两个input / datepicker字段的焦点都会进入某种循环,从一个循环到另一个循环。任何方式:不起作用。我这个问题已经工作了很长时间,现在是时候提出这个问题了。如何使一切工作?最新代码如下。 isDate()是我们编写的函数,如果date无效则返回false。谢谢

.cshtml文件

<span style="white-space:nowrap;">Date Range:<input type="text" class="datepicker" id="dateRangeFrom" value="" /> 
to
<input type="text" class="datepicker" id="dateRangeTo" value="" />


@section scripts{
    <script type="text/javascript">

    $(document).ready(function () {


    $("#dateRangeFrom").datepicker({
            changeMonth: true,
            changeYear: true            
    }).mask('99/99/9999');

    $("#dateRangeTo").datepicker({ 
            changeMonth: true, 
            changeYear: true
    }).mask('99/99/9999');

});

</script>
}

.js文件

$("#dateRangeFrom").datepicker({

    /* fix buggy IE focus functionality */
    fixFocusIE: false,

    onClose: function () {

        this.fixFocusIE = true;

        if (!isDate(this.value)) {

            //focus is set to the End of dateRangeTo and stays there 
            // and both fields are flashing yellow background
            $(this).effect("highlight", {}, "medium").val("");
            $(this).focus();

            setTimeout(function () {
                //$("#dateRangeFrom").focus();  //focus is set to the End of dateRangeTo and stays there 
                //$(this).focus();   // focus is Not set back to related input field
            }, 0);
        }
    }, 
    beforeShow: function () {
        var result = $.browser.msie ? !this.fixFocusIE : true;
        this.fixFocusIE = false;
        return result;
    }

});

$("#dateRangeTo").datepicker({

    /* fix buggy IE focus functionality */
    fixFocusIE: false,

    onClose: function () {

        this.fixFocusIE = true;

        if (!isDate(this.value)) {

            //focus is set to the End of dateRangeTo and stays there 
            // and both fields are flashing yellow background
            $(this).effect("highlight", {}, "medium").val("");
            $(this).focus();

            setTimeout(function () {
                //$("#dateRangeTo").focus();  // see comments above
                //$(this).focus();    // focus is Not set back to related input field
            }, 0);
        }            
    }, 
    beforeShow: function () {
        var result = $.browser.msie ? !this.fixFocusIE : true;
        this.fixFocusIE = false;
        return result;
    }

});

0 个答案:

没有答案