最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Ajax request to create element and appendChild - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Jul 18, 2011 at 17:03 Jonah KatzJonah Katz 5,29817 gold badges69 silver badges91 bronze badges 2
  • This is a duplicate of stackoverflow./questions/6736165/… ... – jbabey Commented Jul 18, 2011 at 17:21
  • Where my answer should be good... – ChristopheCVB Commented Jul 18, 2011 at 17:37
Add a ment  | 

1 Answer 1

Reset to default 2

It should be:

document.getElementById("org_div1").appendChild(div);

and

var div = document.createElement('div');

See:

  • https://developer.mozilla/En/AppendChild
  • https://developer.mozilla/en/document.createElement

From above reference (syntax):

 var child = element.appendChild(child);

 var element = document.createElement(tagName);
发布评论

评论列表(0)

  1. 暂无评论