2014-06-24 46 views
0

我需要一個數組,只存儲來自給定選項的選定值在連續位置。當我運行下面的代碼時,所有的值都存儲在相同的位置(0),而其餘的數組是空的。推數組中的值jQuery

以下是我的html:

<div> 
     <table id="myTable"> 
      <tbody> 
       <tr> 
        <th>Robot By</th> 
        <th>Robot Name</th> 
        <th>Status</th> 
        <th>ADD</th> 
        <th>REMOVE</th> 
       </tr> 
       <tr id="col1"> 
        <td>Google</td> 
        <td><em>googlebot</em></td> 
        <td> 
        <select class="myOption"> 
         <option value="Same as Default">Same as Default</option> 
         <option value="Allowed">Allowed</option> 
         <option value="Refused">Refused</option> 
        </select></td> 
        <td> 
        <button type="button" class="addC"> 
         Add 
        </button></td> 
        <td> 
        <button type="button" class="removeC"> 
         Remove 
        </button></td> 
       </tr> 
       <tr id="col2"> 
        <td>MSN Search</td> 
        <td><em>msnbot</em></td> 
        <td> 
        <select class="myOption"> 
         <option value="Same as Default">Same as Default</option> 
         <option value="Allowed">Allowed</option> 
         <option value="Refused">Refused</option> 
        </select></td> 
        <td> 
        <button type="button" class="addC"> 
         Add 
        </button></td> 
        <td> 
        <button type="button" class="removeC"> 
         Remove 
        </button></td> 
       </tr> 
       <tr id="col3"> 
        <td>Yahoo</td> 
        <td><em>yahoo-slurp</em></td> 
        <td> 
        <select class="myOption"> 
         <option value="Same as Default">Same as Default</option> 
         <option value="Allowed">Allowed</option> 
         <option value="Refused">Refused</option> 
        </select></td> 
        <td> 
        <button type="button" class="addC"> 
         Add 
        </button></td> 
        <td> 
        <button type="button" class="removeC"> 
         Remove 
        </button></td> 
       </tr> 
       <tr id="col4"> 
        <td>Ask/Teoma</td> 
        <td><em>teoma</em></td> 
        <td> 
        <select class="myOption"> 
         <option value="Same as Default">Same as Default</option> 
         <option value="Allowed">Allowed</option> 
         <option value="Refused">Refused</option> 
        </select></td> 
        <td> 
        <button type="button" class="addC"> 
         Add 
        </button></td> 
        <td> 
        <button type="button" class="removeC"> 
         Remove 
        </button></td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
      <input type="button" value="Create Robots.txt" id="createFile"> 
      <input type="reset" value="Clear All" id="clearAll"> 

以下是我的jQuery:

$("#createFile").click(function() { 

    $('#myTable tr').find('td').each(function(index) { 
     $(this).find('.myOption').each(function(){ 

      var myArray = []; 
      myArray.push($(".myOption option:selected").text()); 

}); 
+3

您正在爲每個選項創建一個新數組 – Vatev

+0

記住變量對於JavaScript中的封閉函數是本地的。 – tadman

回答

2

試試這個(demo):

$("#createFile").click(function() { 
    var values = []; 

    $('#myTable .myOption').each(function(){ 
     values.push($(this).val()); 
    }); 

    console.log(values); 
}); 

#myTable .myOption選擇實際上是不夠的。 .val將返回選定的選項值。

+0

謝謝@YuryTarabanko ... – rippy

+0

不客氣。 :) –