Forgive me if this has been answered, but I have searched this site using creative versions of different search terms and came up blank.
I have a php
webpage (index.php), that uses ajax
to load another php
page (dataQuery.php) which has an html
form on it. I have created a small script ( addInput()
) that allows the user to dynamically add as many input fields to this form as they like. One of these fields is a date field that I would like to have a datepicker attached to. In the past I have included any javascript
to the ajax
callback, however in this case I could not get it to work.
Here is my function 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();
}
Here is my function 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=\"\">";
}
I don't know if this is possible, but I have a feeling it is. Appreciate any help.
There are some errors with your code, you have missed the newlabour_date for the generated input field and table.insertRow(rowcount);
giving out of rage error.
A working example without Ajax: http://jsfiddle.net/qxarbpcc/
Here is the updated version of your addInput function:
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=\"\">";
}
Then change your Ajax callback to this:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("container").innerHTML=xmlhttp.responseText;
$('body').on('focus',".newlabour_date", function(){
$(this).datepicker();});
}
Don't forget to add jquery and jqueryui in head section.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.