当我在innerhtml中使用日期选择器功能时,日期选择器不起作用

时间:2016-03-28 07:34:08

标签: jquery html

Html代码:

var td5 = document.createElement("TD")
var strHtml5 = "<From><INPUT Id=\"one\" CLASS=\"datepicker\" type=\"Text\" readonly=\"true\">";
td5.innerHTML = strHtml5.replace(/!count!/g,count);

var td6 = document.createElement("TD")
var strHtml6 = "<To><INPUT Id=\"two\" CLASS=\"datepicker\" type=\"Text\" readonly=\"true\">";
td6.innerHTML = strHtml6.replace(/!count!/g,count);

这是日期选择器功能

$(function() {
   $('input').filter('.datepicker').datepicker({
      changeMonth: true,     
      changeYear: true,
      depth: "year",
      showOn: 'both',      
      buttonImage:'../../resources/theme1/image/calendar.png',
      buttonImageOnly: true,
      disabled: true,
     });
});

1 个答案:

答案 0 :(得分:0)

  

您可以将datepicker初始化对象存储为全局变量,并且可以在以后添加新的datepicker元素时使用它。

试试这个:

&#13;
&#13;
//GLOBAL_INIT will hold the initialization in global context to be used later
var GLOBAL_INIT = {
  changeMonth: true,
  changeYear: true,
  depth: "year",
  showOn: 'both',
  buttonImage: '../../resources/theme1/image/calendar.png',
  buttonImageOnly: true,
  disabled: true,
};


var td5 = document.createElement("TD")
var strHtml5 = "<From><INPUT Id=\"one\" CLASS=\"datepicker\" type=\"Text\" readonly=\"true\">";
td5.innerHTML = strHtml5.replace(/!count!/g, count);

var td6 = document.createElement("TD")
var strHtml6 = "<To><INPUT Id=\"two\" CLASS=\"datepicker\" type=\"Text\" readonly=\"true\">";
td6.innerHTML = strHtml6.replace(/!count!/g, count);
//Will initialize when elements are appended
$(strHtml5).datepicker(GLOBAL_INIT);
$(strHtml6).datepicker(GLOBAL_INIT);


//Will initialize `'.datepicker'` elements initially
$(function() {
  $('input').filter('.datepicker').datepicker(GLOBAL_INIT);
});
&#13;
&#13;
&#13;