简体   繁体   English

页面加载后动态添加jquery datepicker

[英]Add jquery datepicker dynamically after page load

Upon a button click, I have some jquery that will add some html to the 'body' element. 单击按钮后,我有了一些jquery,它将向'body'元素添加一些html。 In that html, I have a datepicker input field which refuses to appear when it should. 在该html中,我有一个datepicker输入字段,该字段拒绝在应有的时候出现。

I use the ui-datepicker in several other spots in my app, and it works perfectly. 我在应用程序的其他几个位置使用了ui-datepicker,它运行良好。

I have tried calling the datepicker when the document loads: 我尝试在文档加载时调用datepicker:

jQuery ->
  $('.my-datepicker-class').each ->
    input = $(this)
    input.datepicker()

and after the click event which adds the new html to the page: 在点击事件之后,它将新的html添加到页面中:

$('.my-button').click ->
  $('body').ipadoverlay
    titleStyle: false
    contentWidth: 500
    topSpc: true
    content: $('#popup-for-' + thing_id).html()
  $('.my-datepicker-class').each ->
    input = $(this)
    input.datepicker()

But the picker refuses to show up. 但是采摘者拒绝露面。 My guess is that it has to do with when jquery builds and places the datepicker, but I really don't know. 我的猜测是,它与jquery构建和放置datepicker的时间有关,但我真的不知道。

Let me know if you have any ideas or could use more information. 让我知道您是否有任何想法或可以使用更多信息。

UPDATE I changed the way I call datepicker() so that it only applies to the element I want the datepicker attached to. UPDATE我更改了调用datepicker()的方式,使其仅适用于我希望将datepicker附加到的元素。

$('.ipadoverlay .date-picker').datepicker()

This eliminates the first issue with the .each =>. 这消除了.each =>的第一个问题。
Now it silently fails. 现在它默默地失败了。 No error or warning. 没有错误或警告。 The datepicker simply does not show up. 日期选择器根本不会显示。

I am going to begin digging into jquery.ui.datepicker.js to see if I can figure out whats going on, but if you have any ideas why the datepicker fails to response, please let me know. 我将开始研究jquery.ui.datepicker.js,以查看是否可以解决问题,但是如果您有任何想法,为什么datepicker无法响应,请告诉我。

UPDATE 2 Ok, I got a step further. 更新2好的,我又走了一步。 The class 'hasDatepicker' was already attached to the element I want add the datepicker to (which means $('.date-picker').datepicker() was being called somewhere else, while the element was hidden and unused), but the datepicker didn't work. 类'hasDatepicker'已经附加到我要添加datepicker的元素上(这意味着$('。date-picker')。datepicker()在其他地方被调用,而该元素被隐藏且未使用),但是datepicker无效。 I fixed this by removing the class from my element, and calling .datepicker() again 我通过从元素中删除类并再次调用.datepicker()来解决此问题

$('.ipadoverlay .date-picker').removeClass('hasDatepicker')
$('.ipadoverlay .date-picker').datepicker()

Now the datepicker shows up on click, but selecting a date doesn't do anything... Help would be much appreciated 现在,单击时将显示日期选择器,但是选择日期没有任何作用...非常感谢您的帮助

You need to use the fat arrow. 您需要使用粗箭头。 Your use of 'this' loses its context. 您对“ this”的使用会失去上下文。

jQuery ->
  $('.my-datepicker-class').each => # <-- Fat arrow
    input = $(this)
    input.datepicker()

I'd explain, but coffeescript.org does a better job than I ever could. 我会解释,但是coffeescript.org的工作比以前更好。

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

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