简体   繁体   English

jQuery UI DatePicker在第二次调用时挂起

[英]jQuery UI DatePicker Hangs on second call

I'm creating an ASP.NET MVC3 app that uses jQuery Datepicker and Timepicker inside a dialog. 我正在创建一个ASP.NET MVC3应用程序,它在对话框中使用jQuery Datepicker和Timepicker。 The code is pretty simple, just localization: 代码非常简单,只需本地化:

$(document).ready(function () {
    $('.datepicker').datepicker({
        dateFormat: "dd/mm/yy",
        monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
        dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
        dayNamesMin:['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
    });

    $('.timepicker').timepicker({
        timeOnlyTitle: 'Titulo',
        timeText: 'Tempo',
        hourText: 'Hora',
        minuteText: 'Minuto',
        secondText: 'Segundo',
        currentText: 'Atual',
        closeText: 'Fechar'
    }); 
});

No secret here. 这里不是秘密。

The datepicker works fine when used for the first time. 第一次使用时,datepicker工作正常。 When I used a second time, the browser (any browser) hangs and offers me to stop script execution of jquery-1.6.4.min.js. 当我第二次使用时,浏览器(任何浏览器)挂起并让我停止jquery-1.6.4.min.js的脚本执行。 To reproduce the error, I just reload the whole page. 要重现错误,我只需重新加载整个页面。

What am I missing here? 我在这里错过了什么?

Update 更新

Adding code for the modal: 为模态添加代码:

First, I configure that everything with class='modal' will have some basic parameters: 首先,我配置class ='modal'的所有内容都有一些基本参数:

$('.modal').dialog({
    resizable: false,
    draggable: false,
    modal: true,
    position: 'center',
    autoOpen: false
});

Then, I extend jQuery with some functions. 然后,我用一些函数扩展jQuery。 One of them sets the buttons and submits: 其中一个设置按钮和提交:

$.extend({
    modalPopup: function (modal) {
        var $modal = $('#' + modal);
        var $form = $modal.find('form').first();

        $modal.dialog({
            buttons: {
                "OK": function (e) {
                    $.validator.unobtrusive.parse($form);
                    if ($form.valid()) {
                        $('.ui-dialog button:contains("OK")').button('disable');
                        $.post($form.attr('action'),
                        $form.serialize(),
                        function (data) {
                            $modal.dialog('close');
                            $('#maindiv').load(telaAtual);
                        });
                    }
                },
                "Cancelar": function () { $(this).dialog("close"); }
            },
            open: function () {
                $modal.unbind('submit');
                $modal.submit(function () {
                $modal.parents('.ui-dialog').first().find('.ui-button').first().click();
                return false;
            });

            $(this).find('.ui-dialog :input').first().blur();
        }
    })
    .dialog('open');
}

}) })

UPDATE UPDATE

I did some research and found that the problem is with DatePicker and Ajax. 我做了一些研究,发现问题出在DatePicker和Ajax上。 Everytime Maybe the Datepicker is "double called" everytime an ajax call is made. 每次可能每次进行ajax调用时,Datepicker都会被“双重调用”。 Something very similar to this question . 这个问题非常相似的东西。 But the Datepicker hangs even if I just close the dialog, meaning that the problem starts when the first ajax call is made. 但即使我关闭对话框,Datepicker也会挂起,这意味着在第一次ajax调用时问题就会出现。

Anyone can help me to fix this? 有人可以帮我解决这个问题吗? Maybe returning false somewhere or destroying the datepicker before creating a new one. 也许在某处返回false或在创建新的之前销毁datepicker。

UPDATE 01/12/2012 更新01/12/2012

Sorry for the delay, guys and thanks for the help. 抱歉延迟,伙计们,感谢您的帮助。 None of the solutions posted here worked. 这里发布的解决方案都没有奏效。 But, again, I thank you all for the help. 但是,我再次感谢大家的帮助。

I found a boolean property $.datepicker.initialized that returns false on the first time I load the dialog, and returns true the second time. 我找到了一个布尔属性$ .datepicker.initialized ,它在我第一次加载对话框时返回false,并在第二次返回true。 Now I can avoid the crash the second time. 现在我可以第二次避免崩溃了。 First problem solved. 第一个问题解决了 But I still don't know how to "reinitialize" the datepicker so it can be shown when the textbox is clicked. 但我仍然不知道如何“重新初始化”日期选择器,以便在单击文本框时显示它。

Still looking for a way to reinitialize it. 仍在寻找重新初始化的方法。

Also, changed the OK button code to this and works fine: 此外,将确定按钮代码更改为此并正常工作:

"OK": function (e) {
                $.validator.unobtrusive.parse($form);
                if ($form.valid()) {
                    $modal.parents('.ui-dialog').find('button:contains("OK")').button('disable');
                    $.post($form.attr('action'),
                        $form.serialize(),
                        function (data) {
                            if (submodal) {
                                carregaParcial(titulo, id);
                            }
                            else {
                                $('#maindiv').html(data);
                            }
                            removeModal($modal);
                        });
                }

The $form.serialize() function already returns the html in the data variable, so, I just need to get its content and set as HTML of the maindiv. $ form.serialize()函数已经返回数据变量中的html,因此,我只需要获取其内容并设置为maindiv的HTML。

提交对话框或取消对话框时,请尝试使用此jquery datetimepicker函数。

  $('.datepicker').datepicker("destroy"); 

You are using the "load" method incorrectly for your purposes. 您正在使用“加载”方法错误地用于您的目的。 jQuery default behavior for load method when no selector is passed is to go to the URL specified, execute the JavaScript that exists on that page then load in the DOM into the jQuery object calling it. 当没有传递选择器时,load方法的jQuery默认行为是转到指定的URL,执行该页面上存在的JavaScript然后将DOM加载到调用它的jQuery对象中。

If you pass a selector into your load method, the jQuery load method will not execute the script and will only load the dom in you jQuery object. 如果将选择器传递给load方法,jQuery加载方法将不会执行脚本,只会在jQuery对象中加载dom。

Change it to: 将其更改为:

$('#maindiv').load(telaAtual + ' #maindiv > *')

This assumes your "telaAtual" URL has #maindiv on the page and then gets all of its children and loads them into "#maindiv" jQuery object. 这假设你的“telaAtual”URL在页面上有#maindiv,然后获取它的所有子节点并将它们加载到“#maindiv”jQuery对象中。

We can go further and find out the particulars of the hanging but solving the load issue will be the first thing to consider. 我们可以进一步了解悬挂的细节,但解决负载问题将是首先要考虑的问题。 If it continues hanging it is worth further investigation. 如果它继续悬挂,值得进一步调查。

Just a guess, not an answer: 只是猜测,而不是答案:

Try to remove the dialog, not just close it. 尝试删除对话框,而不仅仅是关闭它。

...
$modal.dialog('close');
$modal.remove();
...

Maybe that helps? 也许这有帮助吗?


BTW: With this you are clicking on the "OK" button by opening the dialog, don't you? 顺便说一句:有了这个,你打开对话框点击“确定”按钮,不是吗?

$modal.parents('.ui-dialog').first().find('.ui-button').first().click();

And then you load some content, so that happens when the dialog is opening? 然后你加载一些内容,以便在对话框打开时发生?

$('#maindiv').load(telaAtual);

And if the telaAtual returns content which will open the dialog again, you are maybe stucked in an endless loop? 如果telaAtual返回的内容会再次打开对话框,那么你可能会陷入无休止的循环中?

In this code the only very minor syntax issue might be the last unnecessary comma after the dayNamesMin. 在此代码中,唯一非常小的语法问题可能是dayNamesMin之后的最后一个不必要的逗号。 Shouldn't cause any hanging, but remove it and give it a try. 不应该造成任何悬挂,但要将其取下并试一试。 Other than that this code looks fine, so the issue is somewhere else. 除了这个代码看起来很好,所以问题是在其他地方。

I've the same issue here, and in my case, that solution worked: 我在这里遇到同样的问题,就我而言,该解决方案有效:

http://forum.jquery.com/topic/datepicker-in-modal-dialog-problem-with-populating-a-date-field http://forum.jquery.com/topic/datepicker-in-modal-dialog-problem-with-populating-a-date-field

I just assign a random string to datepicker's input ID. 我只是为datepicker的输入ID分配一个随机字符串。

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

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