在用户脚本中使用时,jQuery DatePicker不会更改月/年

时间:2012-01-06 18:17:13

标签: jquery datepicker userscripts

我想出了如何显示月份和年份下拉框但是当我选择它们时没有任何反应。基本上,如果您选择新月/年,则应更改为新月/年。我确定我做错了什么,但到目前为止我已经做到了:

$("input[name=myDate]").datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: false,
        dateFormat: 'm/d/yy'
);

警报甚至没有开火。任何帮助表示赞赏。

更新:

我在下面添加了某人给我的代码,我需要使用这些代码才能使日期选择起火。如果我不使用此代码,那么当我选择一天时,它不会进入文本框。也许这会影响月/年的选择?

$("input[name=myDate]").click(function () {
    setTimeout(cleanUpCrappyEventHandling, 100);
});

function cleanUpCrappyEventHandling() {
    var nodesWithBadEvents = $(
        "div.ui-datepicker td[onclick^='DP'], div.ui-datepicker a[onclick^='DP']"
    );
    nodesWithBadEvents.each(function () {
        var jThis = $(this);
        var fubarFunc = jThis.attr("onclick");

        /*--- fubarFunc will typically be like:
        DP_jQuery_1325718069430.datepicker._selectDay('#pickMe',0,2012, this);return false;
        */
        fubarFunc = fubarFunc.replace(/return\s+\w+;/i, "");

        jThis.removeAttr("onclick");
        jThis.click(function () {
            eval(fubarFunc);
            cleanUpCrappyEventHandling();
        });
    });
}

谢谢!

2 个答案:

答案 0 :(得分:1)

由于这是在用户/扩展程序环境中,因此您需要将cleanUpCrappyEventHandling()扩展为下拉框的帐户。 (我的其他问题的代码不包含这样的原因,因为(1)我没有使用该功能和(2),试图使示例变得过于复杂,除非已知需要它。)

无论如何,用以下内容替换cleanUpCrappyEventHandling(),下拉列表也应该起作用。 :

function cleanUpCrappyEventHandling () {
    //-- Fix base controls.
    var nodesWithBadEvents  = $(
        "div.ui-datepicker td[onclick^='DP'], div.ui-datepicker a[onclick^='DP']"
    );
    nodesWithBadEvents.each ( function () {
        fixNodeEvents ($(this), "click");
    } );

    //-- Fix month and year drop-downs.
    nodesWithBadEvents  = $(
        "div.ui-datepicker select[onchange^='DP']"
    );
    nodesWithBadEvents.each ( function () {
        fixNodeEvents ($(this), "change");
    } );
}

function fixNodeEvents (jNode, eventType) {
    var onName      = "on" + eventType;
    var fubarFunc   = jNode.attr (onName);

    /*--- fubarFunc will typically be like:
        DP_jQuery_1325718069430.datepicker._selectDay('#pickMe',0,2012, this);return false;
    */
    fubarFunc       = fubarFunc.replace (/return\s+\w+;/i, "");

    jNode.removeAttr (onName);
    jNode.bind (eventType, function () {
        eval (fubarFunc);
        cleanUpCrappyEventHandling ();
    } );
}



请注意,userscript / extension环境与普通的页面上下文JS明显不同。这就是为什么另一个答案适用于普通页面但不适用于用户脚本的原因。请记住这些问题,并写下并标记问题。

答案 1 :(得分:0)

更改日历中的月份和年份会自动发生在日期选择器中,您不需要代码为您执行此操作:

$("input[name=myDate]").datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: false,
    dateFormat: 'm/d/yy'
    }
);

另外,我不确定我是否理解onChangeMonthYear事件中发生的事情。根据当前文档,monthyear不是选项。即使它们是,你将它们设置为真,而不是新值。

以下是一个示例:http://jsfiddle.net/g6EXh/