如果这个问题已得到解答,请原谅我,但我使用不同搜索词的创意版本搜索了此网站,但结果是空白。
我有一个 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_date
和 table.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/