使用appendChild不工作在Ajax请求工作、appendChild、Ajax

2023-09-11 00:38:52 作者:╭╮萌男︶ㄣ

我有一个脚本来添加一个输入域元素时,当前是​​pssed $ P $。当我使用的innerHTML它替换当前的,这不是我想要的。所以我想的appendChild应该补充而不是取代,但它不工作。这是我的脚本。

I have a script to add an input field element when a current one is pressed. When i use innerHTML it replaces the current, which is not what i want. So I figured appendChild should add and not replace, but its not working. Heres 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)
    {
    document.getElementById("org_div1").appendChild = xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","addfile.php");
xmlhttp.send();
}
</script>

和我的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>

和我addfile.php文件。

And my addfile.php file..

    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()" />;

任何输入?此外,innerHTML的不工作,使用appendChild没有。谢谢你。

Any input? Again, innerHTML DOES work, appendChild does not. Thanks.

推荐答案

parent.appendChild()正在等待れ对象的函数,你传递一个字符串,它不能正常工作......

parent.appendChild() is a function waiting for DomNode Object and you are passing a String, it can't work...

尝试:

var d = document.createElement('div');
d.innerHtml = xmlhttp.responseText;
document.getElementById("org_div1").appendChild(d);