1

複数の列を持つテーブルがあり、1 つの列がチェックボックスになっています。これがチェックされると、テーブルの右側に別の列セルが表示され、値を設定するためのアップ/ダウン スピナーが表示されます。

私が抱えている問題は、私が持っている数量列にある数字を超えてスピナーを動かすことができないということです. したがって、対応する行の Quantity 列の現在の値を JavaScript 変数に取り込み、それをスピナー関数で使用する必要があります。を使用して値を取得できましたが、getElementByIdそれはテーブルの最初の値のみを取得し、テーブルのそれ以降の値に対しては機能しません。私は努力を続けてgetElementsByClassNameいますが、運がありません。

どうすればこれを成功させることができますか?

PHP/HTML:

<table id="merchTable" cellspacing="5" class="sortable">
    <thead>
        <tr class="ui-widget-header">
            <th class="sorttable_nosort"></th>
            <th class="sorttable_nosort">Loc</th>
            <th class="merchRow">Report Code</th>
            <th class="merchRow">SKU</th>
            <th class="merchRow">Special ID</th>
            <th class="merchRow">Description</th>
            <th class="merchRow">Quantity</th>
            <th class="sorttable_nosort">Unit</th>
            <th style="display: none;" class="num">Quantity #</th>
        </tr>
    </thead>
    <tbody>

        <?php foreach ($dbh->query($query) as $row) {?>

        <tr>
            <td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
            <td class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
            <td class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
            <td class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
            <td class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
            <td class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
            <td class="quantity ui-widget-content" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
            <td class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
            <td style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
        </tr>

    <?php } ?>

    </tbody>
</table>

JavaScript ...変数を使用して値を取り込もうとしていquantityます。チェックボックスを選択するたびに、次のメッセージが表示console.log(quantity)されundefinedます。

$(function () {
    $(".check").change(function(){
    $(this).closest('tr').find('td.quantity_num').toggle(this.checked);
    console.log($('input.check').is(':checked'));
    var quantity = document.getElementsByClassName('quantity').innerHTML;
        console.log(quantity);

  if($('input.check').is(':checked'))
    $(this).closest('table').find('th.num').toggle(true);
    else
    $(this).closest('table').find('th.num').toggle(false);



    $( ".spinner" ).spinner({
      spin: function( event, ui ) {
        if ( ui.value > quantity ) {
          $( this ).spinner( "value", quantity );
          return false;
        } else if ( ui.value <= 0 ) {
          $( this ).spinner( "value", 0 );
          return false;
        }
      }
    });
  });
});
4

3 に答える 3

5
var quantity = $(this).closest('tr').find('td.quantity').html();

その値をデータ プロパティに入れることをお勧めします。

<td class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>

そして、jQuery からその値にアクセスできます。

var quantity = $(this).closest('tr').find('td.quantity').data('quantity');

これにより、HTML に依存しなくなります。明日、そのセルで a を使用する<span>か、数量に単位を追加するとします。データがプロパティ内にある場合、実際にセル内にあるものに依存しません。

于 2017-05-10T12:33:07.193 に答える