2016-12-06 59 views
0

我有以下php表和js函數,我一直在努力。問題是,索引($ ind)在php中正常工作,但我無法弄清楚js函數中的語法。 var ind = $('id')[ind];的輸出未定義。當我替換並設置var ind =;它爲每個項目提供了數組中的最後一個索引值。我可以設置var ind等於在javascript中捕獲PHP的$ ind值嗎?在javascript函數中無法獲得正確的索引值語法

<table id="manage-items" cellpadding="0" cellspacing="0" border="0"> 
     <thead> 
     <tr class="search"> 
      <th>&nbsp;</th> 
      <th><?php echo $this->translate('Quantity');?></th> 
      <th><?php echo $this->translate('Status'); ?></th> 
     </tr> 
     </thead> 

     <?php $ind = 0; ?> 
     <?php foreach ($this->items as $item) { 
    $item_link = 'type=product'; 
    ?> 

     <div id="item_<?php echo $ind; ?>" style="display:none;"> 

     <tr id="<?php echo $item['id']; ?>"> 
      <td>&nbsp;</td> 

      <td class="Quantity"> 
      <?php if ($item->item_quantity) { ?> 
      <span class="silvertext"><?php echo $item->item_quantity; ?></span> 
      <?php } else { ?> 
      <span class="silvertext">0</span> 
      <?php } ?> 
      </td> 

      <td class="Status"> 

      <?php if (in_array($item['active'], array(0, 1))) { ?> 
      <div class="switch"> 

       <label for="active[<?php echo $ind; ?>]" 
        class="switch-label switch-label-off">Active</label> 
       <input type="radio" class="switch-input" 
        id="active[<?php echo $ind; ?>]" 
        name="item[<?php echo $ind; ?>][status]" 
        value="1" <?php if ($item['active'] == 1) echo 'checked'; ?>> 

       <label for="inactive[<?php echo $ind; ?>]" 
        class="switch-label switch-label-on">Inactive</label> 
       <input type="radio" class="switch-input" 
        id="inactive[<?php echo $ind; ?>]" 
        name="item[<?php echo $ind; ?>][status]" 
        value="0" <?php if ($item['active'] == 0) echo 'checked'; ?>> 

       <span class="switch-selection"></span> 
      </div> 
      <?php } else { ?> 
      <?php echo $item['active']; ?> 
      <?php } ?> 
      </td> 
     </tr> 
     <?php $ind++; ?> 
     <?php } ?> 
     </tbody> 
    </table> 

    <script type="text/javascript"> 
     $(document).ready(function() { 

     if (typeof ind == "undefined") { 
      ind = 0; 
     } 
     $('input[type="radio"]').live('change', function() { 
      var status = this.value; 
      var id = $(this).parents('tr').attr('id'); 
      var quantity = $(this).closest("tr").find(".Quantity > span").text(); 
      var ind= $('id')[ind]; 

      // a bunch of other javascript 

     }); 
     }); 
    </script> 
+0

改變,你可以提供一些HTML源代碼(PHP的輸出)?你檢查過控制檯嗎? – Daidon

+0

@Daidon,控制檯中沒有錯誤,但是var ind = $('id')[ind];的輸出。未定義。當我替換並設置var ind = <?php echo $ ind; ?>;它爲每個項目提供了數組中的最後一個索引值。 – ian

+0

嘗試typeof ind ===(三等於) – Daidon

回答

1

var id包含您想要訪問的tr的ID。

如果使用.find遍歷id,則可以訪問要更改的跨度。

var target = $("#"+id).find('span')[5]; 

應用,然後在目標

$(target).css("...", "..."); 
+0

沒有工作。沒有控制檯錯誤,但var ind等於每個循環的最後一個值。我注意到var id = $(this).parents('tr')。attr('id');爲每個記錄提供正確的ID。有沒有辦法通過引用id來獲取索引值? – ian

+0

我想問你一些實際的html代碼,所以我可以看到錯誤可能是什麼。目前,我甚至不知道你用$('id')選擇了什麼。你在html中有id標籤嗎? – Daidon

+0

對不起,我很困惑你需要什麼。從上面的html中,我從下面的html行獲得var id,」>。據我所知,如果更容易,我可以粘貼整個頁面,但是要點在上面。 – ian

相關問題