问题
Im trying to make a script to add an input field when a current one is clicked using ajax/appendChild but it is not working. Here is my script..
<script type="text/javascript">
function addfile()
{
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)
{
var s = xmlhttp.responseText;
var div = createElement('div');
div.innerHTML = s;
document.getElementById("org_div1").appendChild = div;
}
}
xmlhttp.open("GET","addfile.php");
xmlhttp.send();
}
</script>
And html..
<div id="org_div1" class="file_wrapper_input">
<input type="hidden" value="1" id="theValue" />
<input type="file" class="fileupload" name="file1" size=
"80" onchange="addfile()" /></div>
And addfile.php file..
<script type="text/javascript">
var i=0;
var ni = document.getElementById('org_div1');
var numi = document.getElementById('theValue');
var i=0;
var ni = document.getElementById('org_div1');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var divIdName = num;
</script>
<input type="file" class="fileupload" size="80" name="file' + (num) + '" onchange="addfile()" />;
Any input? Thanks.
回答1:
It should be:
document.getElementById("org_div1").appendChild(div);
and
var div = document.createElement('div');
See:
- https://developer.mozilla.org/En/AppendChild
- https://developer.mozilla.org/en/document.createElement
From above reference (syntax):
var child = element.appendChild(child);
var element = document.createElement(tagName);
来源:https://stackoverflow.com/questions/6736528/using-ajax-request-to-create-element-and-appendchild