开发者

Adding names to an array and outputting them to a table

I'm having some trouble getting my code to work. This is what I have so far.

function outputNamesAndTotal() {
  var name;
  var outputTable;
  var inputForm;
  var nameArray;
  var outputDiv;

  outputDiv = document.getElementById("outputDiv");
  inputForm = document.getElementById("inputForm");
  outputTable = document.getElementById("outputTable");
  name = inputForm.name.value;
  nameArray = [];

  nameArray.push(name);


  for (var i = 0开发者_如何学C; i > nameArray.length; i++) {

    outputTable.innerHTML += "<tr>" + nameArray[i] + "</tr>";

  }    

  inputForm.name.focus();
  inputForm.name.select();


  return false;

}

When I add the loop it breaks the code completely, but I can't figure out why.

What I'm trying to do is use an HTML form to get a name from the user. Once the user enters the name, the program adds the name to the array, and outputs each array entry to a row in a table.

It's pretty basic, but it's still giving me all kinds of trouble!


I think you are clearing your array of names every time you call the function. You should bring the line:

nameArray = [];

out and make it global.

I ran a quick test and the following code works in at least FireFox

Edited to use appendChild

<html>
<head>
<script type='text/javascript'>
var names = [];
function addName() {
    var nameTxt = document.getElementById('name_txt');
    var name = nameTxt.value;
    names.push(name);
    var outTable = document.getElementById('out_tbl');

    var row = document.createElement('tr');
    var entry = document.createElement('td');
    var txt = document.createTextNode(name);

    entry.appendChild(txt);
    row.appendChild(entry);
    outTable.appendChild(row);


    var numDiv = document.getElementById('num_div');
    removeAllChildren(numDiv);
    var numTxt = document.createTextNode('You have ' + names.length + ' names');
    numDiv.appendChild(numTxt);
}
function removeAllChildren(e) {
    while (e.hasChildNodes()) {
        e.removeChild(e.firstChild);
    }
}
</script>
</head>
<body>
    <table id='out_tbl'>
    </table>
    <div id='num_div'>You have 0 names</div>
    <input id='name_txt' type='text'/>
    <button onclick="addName()">CLICK</button>
</body>
</html>

Edit: Oh yeah and you are the fact that you are looping through the array every time. If you "globalize" the name array, you're gonna print the whole array every time you add a name.

Edit x2: the code you originally posted had nameArray as a local variable inside the function. This effectively clears the array every time you call the function. Then every time you call the function you add the current name to the now empty array, and loop through all 1 (one) elements that the array now holds.

What you want to do is "globalize" the name array, and remove the loop from your function. This will allow you to build up your name array across multiple calls, and works the way that you want it.

Also, innerHTML is not really the best way to add things to the page. I would suggest using appendChild().

-C


for (var i = 0; i > nameArray.length; i++) {

I think you mean i < nameArray.length

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜