我有以下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> </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> </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>
改變,你可以提供一些HTML源代碼(PHP的輸出)?你檢查過控制檯嗎? – Daidon
@Daidon,控制檯中沒有錯誤,但是var ind = $('id')[ind];的輸出。未定義。當我替換並設置var ind = <?php echo $ ind; ?>;它爲每個項目提供了數組中的最後一個索引值。 – ian
嘗試typeof ind ===(三等於) – Daidon