【问题标题】:datepicker onselect is not working [when datepicker div created dynamically]datepicker onselect 不工作 [当 datepicker div 动态创建时]
【发布时间】:2014-10-23 13:30:05
【问题描述】:

我有一个 datepicker 函数,它似乎在工作,但它的 onSelect 函数不工作我看不到 alert()

$(function() {
var divContent = '<input type="text" placeholder="Enter date" class="dupInput" id="gl_img_datepicker" readonly=""><button id="gl_img_datepicker_init">Date</button>';


jQuery('body').on('click', '#gl_img_datepicker_init', function(e) {
        jQuery("#gl_img_datepicker").datepicker('show');
 });

//onselect datepicker function 
jQuery('#gl_img_datepicker').datepicker('option', 'onSelect',
   function(dateText, inst) {
     alert('glImgExpiry-->' + dateText);
});
jQuery('body').on('click', '#mybutton', function(e) {
  jQuery("#my_div").append(divContent)
  jQuery("#gl_img_datepicker").datepicker();        
});

});

查看我的Fiddle


我已经看过以下问题,但没有帮助。

Datepicker onSelect Not Firing

jquery datepicker onselect not working

Datepicker onSelect not working

jQuery Datepicker onSelect is not working

谢谢

【问题讨论】:

  • 不适合我

标签: javascript jquery jquery-ui datepicker


【解决方案1】:

您正在尝试将该事件分配给尚不存在的字段(即在创建 #gl_img_datepicker 并将其插入文档之前)。试试这样(这里是JSFiddle):

$(function() {
    var divContent = '<input type="text" placeholder="Enter date" class="dupInput" id="gl_img_datepicker" readonly=""><button id="gl_img_datepicker_init">Date</button>';


    jQuery('body').on('click', '#gl_img_datepicker_init', function(e) {
            jQuery("#gl_img_datepicker").datepicker('show');
     });

    jQuery('body').on('click', '#mybutton', function(e) {
        jQuery("#my_div").append(divContent)
        jQuery("#gl_img_datepicker").datepicker();    

        // We have already appended the divContent to the document; we
        // can continue with the actual datepicker event for that field

        //onselect datepicker function 
        jQuery('#gl_img_datepicker').datepicker('option', 'onSelect',
           function(dateText, inst) {
             alert('glImgExpiry-->' + dateText);
        });    
    });

});

【讨论】:

  • 感谢@user1680977 的回答----有更好的方法吗? ..在同样的情况下我告诉过?
  • 感谢天才的回答,我希望这是正确的方法。无论如何,对我来说最重要的是,它有效:) ..非常感谢
  • 是的,我会这样做,但是,如果你想拥有多个这样的输入字段,你应该使用.classes而不是#ids
猜你喜欢
  • 2016-06-15
  • 2011-05-14
  • 2010-10-27
  • 1970-01-01
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
相关资源
最近更新 更多