繁体   English   中英

无法在JavaScript函数中获取索引值的正确语法

[英]Cannot get correct syntax for index value in javascript function

我有以下一直在努力的php表和js函数。 问题是,索引($ ind)在php中可以正常工作,但是我无法弄清楚js函数中的语法。 var ind = $('id')[ind]的输出; 未定义。 当我替换并设置var ind =时; 它为我提供了数组中每个项目的最后一个索引值。 我该如何将var ind设置为等于以捕获javascript中php的$ ind值?

    <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>

var id包含您要访问的tr的ID。

如果使用.find遍历id,则可以访问要更改的跨度。

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

然后将更改应用于目标

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM