2013-08-18 84 views
1

JavaScript中的新手,我想创建一个简单的表单,基于用户填充的数组创建项目列表。我如何获得基于数组创建的项目列表x - 提前致谢从Javascript中创建列表

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html> 
    <head> 
    <title></title> 
    <script type="text/javascript"> 
    function checkwarranty() 
    { 
     var x = new Array(); 

     x[0] = document.getElementById('clientname').value; 
     x[1] = document.getElementById('select').value; 
     x[2] = document.getElementById('expirationdate').value; 
     x[3] = document.getElementById('notifyon').value; 
    } 
    </script> 
    </head> 
    <body> 
    <form> 
     <table border="1"> 
     <tr> 
      <td>Client Name:</td> 
      <td> 
      <input type="text" name="clientname" id="clientname"> 
      </td> 
     </tr> 
     <tr> 
      <td>Device Type:</td> 
      <td> 
      <select id="select"> 
       <option value="Server">Server</option> 
       <option value="Server">Firewall</option> 
       <option value="Server">Domain</option> 
       <option value="Server">Desktop</option> 
      </select> 
      </td> 
     </tr> 
     <tr> 
      <td>Warranty Expires on:</td> 
      <td> 
      <input type="date" name="expirationdate" id="expirationdate"> 
      </td> 
     </tr> 
     <tr> 
      <td>Notify On:</td> 
      <td> 
      <input type="date" name="notifyon" id="notifyon"> 
      </td> 
     </tr> 
     </table><br> 
     <input type="button" name="submit" value="submit" onclick="checkwarranty()"> 
    </form> 
    </body> 
</html> 

回答

1

多维数组是你想要的。试试这个

var list = []; 

function checkwarranty() 
{ 
    var x = []; 

    x[0] = document.getElementById('clientname').value; 
    x[1] = document.getElementById('select').value; 
    x[2] = document.getElementById('expirationdate').value; 
    x[3] = document.getElementById('notifyon').value; 

    list.push(x); 
} 

有关更多信息,请http://www.w3schools.com/jsref/jsref_obj_array.asp

+3

大多数人使用'名单= [];'语法,这些天。 –

+0

我发现'var list = new Array();'更具可读性。此外,我想你可以将这四个值推入'list'而不需要绕道创建数组'x'。 – Momro

+0

@momro我认为这个问题是创建一个多维数组。像列表对象列表:-) – rozar