简体   繁体   English

DatePicker 不在模态内工作

[英]DatePicker not working inside a modal

I have a site and here is a link , When you click on textfield you can see the DatePicker working But iF you click on I mportFriend -> Add Manual Friend -> then if you click on birthday field the datepicker never appears.I can see through firebug that the field got the hasDatePicker attribute.我有一个网站,这里有一个链接,当您单击文本字段时,您可以看到 DatePicker 正在工作但是如果您单击 I mportFriend -> Add Manual Friend ->然后如果您单击生日字段,则日期选择器永远不会出现。我可以看到通过萤火虫,该字段获得了hasDatePicker属性。 I am not sure what making it not to show the datepicker,anyone who can help me please thanks我不确定是什么让它不显示日期选择器,任何可以帮助我的人请谢谢

As @Bluetoft says, the answer is event delegation.正如@Bluetoft 所说,答案是事件委托。

The reason it's not working for you is because your datepicker is being bound to elements that exist at the time the script is run (not to mention, the birthday field has a different id than #datepicker, which is what your script is binding to).它对您不起作用的原因是因为您的 datepicker 被绑定到脚本运行时存在的元素(更不用说,生日字段的 ID 与 #datepicker 不同,这是您的脚本绑定到的 ID) .

When you dynamically bring in the new form, the datepicker is not bound to the new elements.当您动态引入新表单时,日期选择器不会绑定到新元素。

So, one method, according to this answer , is to construct your script like so:因此,根据此答案,一种方法是像这样构建脚本:

$(function() {
    $("body").delegate("#datepicker", "focusin", function(){
        $(this).datepicker();
    });
});

Additionally , your form "birthday" input has an id of #fi_bday, which is another reason it's not being bound.此外,您的表单“生日”输入的 ID 为 #fi_bday,这是它没有被绑定的另一个原因。 I'd recommend that you assign any inputs where you want the datepicker with a class of "datepicker", then change your script to bind the datepicker functionality to '.datepicker' elements:我建议您将任何输入分配给您想要使用“datepicker”类的日期选择器,然后更改您的脚本以将日期选择器功能绑定到'.datepicker'元素:

$(function() {
    $("body").delegate(".datepicker", "focusin", function(){
        $(this).datepicker();
    });
});

Finally , if you don't want to utilize the better, generic class method above, you can use two selectors in the selector below.最后,如果你不想使用上面更好的通用类方法,你可以在下面的选择器中使用两个选择器。 The method below delegates in a manner as to bind the datepicker to the modal window element(s) as well:下面的方法以将日期选择器绑定到模态窗口元素的方式进行委托:

$(function() {
    $("body").delegate("#datepicker, #fi_bday", "focusin", function(){
        $(this).datepicker();
    });
});

The solution mentioned above did not work for me.上面提到的解决方案对我不起作用。

As date-picker has not the z-index property, so it was not showing on modal/popup which has the z-index.由于日期选择器没有 z-index 属性,因此它没有显示在具有 z-index 的模式/弹出窗口中。 I found that the date-picker was working fine but it was showing behind the modal.So I added the z-index property to date-picker class.我发现日期选择器工作正常,但它显示在模态后面。所以我将 z-index 属性添加到日期选择器类。

.datepicker{ z-index:99999 !important; }

That's why I am sharing my solution to other users.这就是我将我的解决方案分享给其他用户的原因。 I am sure this will help someone.我相信这会帮助某人。

Add z-index to your datepicker class and add important with it.将 z-index 添加到您的 datepicker 类并添加重要内容。 give a Greater value than modal.给出比模态更大的值。 Normally modal have a z-index of 1050通常模态的 z-index 为 1050

.yourDatePicker{ z-index:9999!important; }

about C#.net in .aspx关于 .aspx 中的 C#.net

function getjqueryselect() {
        $("#<%=this.txtBeginEdit.ClientID%>").datepicker($.datepicker.regional["th"]);
        $("#<%=this.txtBeginEdit.ClientID%>").datepicker("setDate", new Date());
        $("#<%=this.txtEndEdit.ClientID%>").datepicker($.datepicker.regional["th"]);
        $("#<%=this.txtEndEdit.ClientID%>").datepicker("setDate", new Date());
    }

and then in Page_Load in aspx.cs然后在 aspx.cs 中的 Page_Load

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "test", "getjqueryselect();", true);

Add property container:添加属性容器:

$('.datepickerClass').datepicker({
           ...,
            container: "#modalId",
           ...,
        });

I had the same issue and I figured out that I was rendering the datetimepicker library twice (once in the main page and once in the partial view).我遇到了同样的问题,我发现我渲染了 datetimepicker 库两次(一次在主页面中,一次在局部视图中)。 Rendering only once in the main page solved it.只在主页面渲染一次就解决了。

I tried the event delegation snippet/z-index But the select year dropdown funcionality of jquery datepicker just did NOT work - i can select date/month but NOT year我尝试了事件委托片段/z-index 但是 jquery datepicker 的选择年份下拉功能不起作用 - 我可以选择日期/月份但不能选择年份

While date-picker for bootstrap did work - the gijgo one or the tempusdominus (which i did NOT try) - I just wanted to stick to jquery虽然引导程序的日期选择器确实有效 - gijgo one 或 tempusdominus(我没有尝试过) - 我只是想坚持使用 jquery

So i got rid of modal functionality - and used collapse in bootstrap to show/hide/div as well as custom div_onshown/hide/cancel/ok using javascript所以我摆脱了模态功能 - 并在引导程序中使用折叠来显示/隐藏/div 以及使用 javascript 的自定义 div_onshown/hide/cancel/ok

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

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