2013-08-18 122 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