简体   繁体   English

从另一个JS函数启动jQuery .datepicker

[英]Launch jQuery .datepicker from Another JS Function

I need to launch .datepicker only if the text entered into a field does not meet a set of criteria. 仅当输入字段中的文本不符合一组条件时,才需要启动.datepicker。 But, I can not figure out how to launch .datepicker other than directly from a field getting focus or a button being presses. 但是,除了直接从获取焦点或按下按钮的字段之外,我无法确定如何启动.datepicker。

What I'm trying to do... 我正在尝试做的...

The HTML HTML

<input type="text" id="userDate" name="userDate" style="width: 100px;" onblur="checkDate(this.id);" />

The JS JS

function checkDate(theId)
{
    var enteredValue = document.getElementById(theId).value;
    var checkedValue = evaluateValue(enteredValue); // this checks to see if the text meets the criteria

    if (checkedValue == null)
    {
        // this is where I want to call the .datepicker to have the calendar show up
        $("#userDate").datepicker();
    }
    else
    {
        document.getElementById(theId).value = checkedValue;
    }
}

I know, this would be easier if the calendar just came up when the text field gets focus. 我知道,如果在文本字段获得焦点时才出现日历,这会更容易。 But, this is not the way the client wants it to work. 但是,这不是客户希望它工作的方式。 They want to accept user input as long as it falls into their desired formats. 他们希望接受用户输入的内容,只要它们符合所需的格式即可。 If not, then they want the calendar and a default, forced format. 如果不是,则他们需要日历和默认的强制格式。

Thanks. 谢谢。

If you are using the datepicker in jQuery UI, you would show the datepicker like this: 如果您在jQuery UI中使用日期选择器,则应显示如下所示的日期选择器:

$("#userDate").datepicker("show");

Edit: 编辑:

Your code would probably have to look something like this: 您的代码可能必须看起来像这样:

$(function(){
   $("#userDate").datepicker(); // <-- Create the datepicker on load
});

function checkDate(theId)
{
    var enteredValue = document.getElementById(theId).value;
    var checkedValue = evaluateValue(enteredValue); // this checks to see if the text meets the criteria

    if (checkedValue == null)
    {
        // this is where I want to call the .datepicker to have the calendar show up
        $("#userDate").datepicker("show");  // <-- Show datepicker when needed
    }
    else
    {
        document.getElementById(theId).value = checkedValue;
    }
}

Okay, after many cans of Pepsi Max and even more hours of research, trial and error; 好了,经过许多罐百事可乐Max以及更多小时的研究,反复试验; this is what I've come up with... and it meets my needs. 这就是我想出的...它满足了我的需求。

$(document).ready( function()
    {
        $("#userDate").blur(function()
        {
            var convertResults = convertMyDate('userDate'); // call to my code that checks the date for accepted formats

            if (convertResults == null)
            {
                $("#userDate").datepicker({
                    changeMonth: true,
                    changeYear: true
                });
                $("#userDate").datepicker("show");
            }
            else
            {
                document.getElementById('userDate').value = convertResults.toString('MM/dd/yyyy');
            }
        });
    });

The only problem now is that the client's form has 14 date fields. 现在唯一的问题是客户的表单有14个日期字段。 So... copy this 13 more times in my code!? 所以...在我的代码中再复制13次! I just don't have the time right now to figure out a way to roll all this into a single function that will initiate all of them in a loop... 我只是现在没有时间想办法将所有这些转换为单个函数,从而将所有这些函数循环启动...

Any suggestions on that would be appreciated. 任何建议,将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM