javascript - 如何将 jquery datepicker 添加到动态创建的输入(位于名为 page 的 ajax 中)

标签 javascript php jquery ajax datepicker

如果这个问题已得到解答,请原谅我,但我使用不同搜索词的创意版本搜索了此网站,但结果是空白。

我有一个 php 网页 (index.php),它使用 ajax 加载另一个 php 页面 (dataQuery.php),其中包含上面有一个 html 表单。我创建了一个小脚本 (addInput()),允许用户根据需要动态向此表单添加任意数量的输入字段。其中一个字段是日期字段,我想附加一个日期选择器。过去,我已将任何 javascript 包含到 ajax 回调中,但在本例中我无法让它工作。

这是我的函数 Query():

function Query()    
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    { 
    document.getElementById("container").innerHTML=xmlhttp.responseText;

    // Tried this suggestion found on stackoverflow:
    $('body').on('focus',".newlabour_date", function(){
    $(this).datepicker();});

    // And this one:
    $( "#newlabour_date" ).datepicker();
    }
  }
xmlhttp.open("GET","/process/dataQuery.php",true);
xmlhttp.send();
}

这是我的函数addInput()

function addInput(table,counter,rowcount)
{
var counter = document.getElementById(counter);
var number = counter.value;
counter.value = ++counter.value;

var rowcount = (rowcount*1+1); // muliply the rowcount by 1 to ensure it doesn't get treated as a concatenation.

var table = document.getElementById(table);
var row = table.insertRow(rowcount);
row.align = "center";

var cell1 = row.insertCell(0);
cell1.className = "bBottom";
cell1.width = "20%";
cell1.height = "21";

cell1.innerHTML = "<input type=\"text\" style=\"position:relative; top:2px;\"
class=\"noborder\" name=\"newlabour_date["+number+"]\" id=\"newlabour_date["+number+"]\" size=\"15\" maxlength=\"\" placeholder=\"\" value=\"\">";
}

我不知道这是否可能,但我有一种感觉。感谢任何帮助。

最佳答案

您的代码存在一些错误,您错过了生成的输入字段的newlabour_datetable.insertRow(rowcount); 给出愤怒错误。

没有 Ajax 的工作示例: http://jsfiddle.net/qxarbpcc/

这是 addInput 函数的更新版本:

function addInput(table,counter,rowcount)
{
    var counter = document.getElementById(counter);
    var number = counter.value;
    counter.value = ++counter.value;

    var rowcount = parseInt(rowcount)+1; // muliply the rowcount by 1 to ensure it doesn't get treated as a concatenation.

    var table = document.getElementById(table);
    var row = table.insertRow(0);
    row.align = "center";

    var cell1 = row.insertCell(0);
    cell1.className = "bBottom";
    cell1.width = "20%";
    cell1.height = "21";

    cell1.innerHTML = "<input type=\"text\" style=\"position:relative; top:2px;\" class=\"noborder newlabour_date\" name=\"newlabour_date["+number+"]\" id=\"newlabour_date["+number+"]\" size=\"15\" maxlength=\"\" placeholder=\"\" value=\"\">";
}

然后将 Ajax 回调更改为:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
    document.getElementById("container").innerHTML=xmlhttp.responseText;

    $('body').on('focus',".newlabour_date", function(){
    $(this).datepicker();});
}

不要忘记在 head 部分添加 jquery 和 jqueryui。

关于javascript - 如何将 jquery datepicker 添加到动态创建的输入(位于名为 page 的 ajax 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27075999/

相关文章:

javascript - 使用 immer 在 JavaScript 中复制对象

javascript - Node.js 在 session 到期 Express 和 Passport 上运行函数

php - 在 php.net 上找到的数据库适配器

php - 远程 Ratchet 连接

javascript - 替换刮掉的文本

javascript - Jquery - 获取数据属性文本值

javascript - 使用 tab 启动点击 div 并输入

javascript - 在 Javascript 中对 2 个数组进行排序的简单方法?

php - SQL 中跳过 0 和 NULL

javascript - Puppeteer 同步定义浏览器和页面