jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery datepicker只能工作一次,第二次不显示大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
ASP.NET MVC3 / jQuery 1.9.1 / jQuery UI 1.10.2

我有一个页面,在单击Ajax.ActionLink后打开模态对话框.在这个对话框中,我有一个输入字段和一个与之关联的日期选择器.当我第一次打开对话框时,我可以单击datepicker按钮(或在关联的输入字段内,以便它获得焦点,showOn设置为两者),并且datepicker按预期显示.我可以,当模态对话框打开时,按照我想要的频率执行,每次都会显示日期选择器.当我关闭模态对话框时(通过附加的$(“ui-widget-overlay”).单击(function(){…});然后再次打开它,无论我是否尝试,datepicker都不再有效单击按钮或关联的输入字段.

我尝试调试jQuery代码,并且两次运行的代码行都是相同的(即使第二次打开对话框时没有出现datepicker,也会触发jQuery方法)在调试器中.我完全被难倒了,this post中描述的方法仅在第一次打开对话框时显示日期选择器方面有所帮助. Another post似乎只与误解showOn设置如何工作有关.

我还试图通过$(“#datepicker”)来破坏日期选择器.datepicker(“destroy”);关闭对话框时 – 无济于事.有任何想法吗?

更新

在“呼叫页面”上:

$(document).ready(function () {
    $("#popupDialog").dialog(
    {
        autoOpen: false,modal: true,open: function()
        {
            $("ui-widget-overlay").click(function()
            {
                $("#popupDialog").dialog("close");
            }
        }
    });
});
[...]
@Ajax.ActionLink( "Some text","Action","Controller",new AjaxOptions {
    httpR_808_11845@ethod = "GET",updateTargetId = "popupDialog",InsertionMode = InsertionMode.replace,Onsuccess = "openPopup()"
})
[...]
function openPopup()
{
    $("popupDialog").dialog("open");
}
[...]
<div id="popupDialog" style="display: none;"></div>

控制器动作非常简单,如下所示:

public ActionResult Action()
{
    MyActionModel myActionModel = new MyActionModel();
    return PartialView( myActionModel );
}

解决方法

在进行了更多调试并尝试跟踪jQuery事件之后,我测试了jQuery UI 1.9.2是否存在问题,但它没有.然后我比较了相关的datepicker代码行,这些代码行没有涉及太多的实际更改.

总而言之,上面我的问题中描述的问题可以通过将一行代码从1.10.2更改为1.9.2中的内容解决

1.10.2造成问题

/* Initialise the date picker */
if (!$.datepicker.initialized) {
    $(document).mousedown($.datepicker._checkExternalClick);
    $.datepicker.initialized = true;
}

1.9.2.版本,按预期工作

/* Initialise the date picker */
if (!$.datepicker.initialized) {
    $(document).mousedown($.datepicker._checkExternalClick)
        // !!!!!!!!!!
        // The next code line has to be added again so that the date picker
        // shows up when the popup is opened more than once without reloading
        // the "base" page.
        // !!!!!!!!!!
        .find(document.body).append($.datepicker.dpDiv);
    $.datepicker.initialized = true;
}

我仍然不确定为什么这种行为存在,但它似乎是一个相对罕见的星座.注意:在打开弹出对话框(或通过AJAX请求PartialView)后,我没有“重新初始化”日期选择器,因此将单个脚本源作为_Layout.cshtml的一部分就足够了.希望这有助于其他人.

大佬总结

以上是大佬教程为你收集整理的jQuery datepicker只能工作一次,第二次不显示全部内容,希望文章能够帮你解决jQuery datepicker只能工作一次,第二次不显示所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。