简体   繁体   English

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

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

I have the following php table and js function I've been working on. 我有以下一直在努力的php表和js函数。 The problem is, the index ($ind) works correctly in the php, but I cannot figure out the syntax in the js function. 问题是,索引($ ind)在php中可以正常工作,但是我无法弄清楚js函数中的语法。 The output of var ind= $('id')[ind]; var ind = $('id')[ind]的输出; is undefined. 未定义。 When I substitute and set var ind= ; 当我替换并设置var ind =时; it gives me the last index value in the array for each item. 它为我提供了数组中每个项目的最后一个索引值。 What can I set var ind equal to to capture the $ind value of the php in javascript? 我该如何将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 contains the ID of the tr you want to access. var id包含您要访问的tr的ID。

if you iterate through id with .find, you can access the span that you want to change. 如果使用.find遍历id,则可以访问要更改的跨度。

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

Apply changes then on target 然后将更改应用于目标

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

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

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