3

jquery に問題があります。SQL データベースから配列で結果を取得しています。アイテムは にありtable tr tdます。各行には一意の ID が保持されます。

しかし、合計を実行するか、1 つのテキスト ボックスに対して onkeyup を実行すると、すべての合計も変化します。

onKeyUpのイベントを予定していQtyます。

例えば。:

数量 = 3

ベッドラグ = 20

合計 (予想) = 60

スクリーンショットは次のとおりです。 ここに画像の説明を入力

私のjQueryコードは間違っていますか?

jQuery(document).ready(function(){

    var j = 0;
    /* COMPUTATION new item */
    jQuery('input[id=new_quan_'+j+']').keyup(function(){
        j++;
        var new_sum = parseInt($('input[id=new_quan_'+j+']').val(), 10);
        new_sum *= parseInt($('input[id=new_amt_'+j+']').val(), 10);
        jQuery('input[id=new_total_'+j+']').val(new_sum);
    });
}); 

HTML と PHP コードは次のとおりです。

<table cellpadding="5" cellspacing="0" width="100%" id="computation_table">
    <tbody>
    <tr>
        <th>Categories</th>
        <th>Qty</th>
        <th>Omschrijving</th>
        <th>Bedrag</th>
        <th>Totaal</th>
        <th>BTW</th>
        <th></th>
    </tr>
    <?
    $quo_com = mysql_query( "SELECT * FROM jon_com_quo_computation WHERE user_code = '".$_GET['uc']."' and footnote = 'new' and active='1' " ) or die ( mysql_error() );
    $get_btw = mysql_fetch_array( $quo_com );
    if ( $get_btw['currency'] == 'euro' ) {
        $msg_tot = '&euro;';
    } elseif ( $get_btw['currency'] == 'usd' ) {
        $msg_tot = '&#36;';
    }

    $sqlview = mysql_query( "SELECT * FROM jon_com_quo_computation WHERE user_code = '".$_GET['uc']."' and footnote = 'new' and active='1' " ) or die ( mysql_error() );
    $j = 0;
    while( $row = mysql_fetch_array( $sqlview ) ) {
    ?>
    <tr>
        <td>
            <input type="hidden" name="id_<?=$j?>" value="<?=$row['q_id'];?>" />
            <input type="hidden" name="counter" value="<?=$j?>" />
            <input type="text" name="category_<?=$j?>" class="text_com" value="<?=$row['category'];?>" />
        </td>
        <td>
            <input type="text" name="quantity_<?=$j?>" id="new_quan_<?=$j?>" class="text_com" value="<?=$row['quo_quantity'];?>" /> x
        </td>
        <td width="200">
            <input type="text" name="quo_definition_<?=$j?>" class="input" value="<?=$row['quo_definition'];?>" />
        </td>
        <td>
            <input type="text" name="quo_amt_<?=$j?>" id="new_amt_<?=$j?>" class="text_com" value="<?=$row['quo_amt'];?>" />
        </td>
        <td id="total">
            <input type="text" name="quo_total_<?=$j?>" id="new_total_<?=$j?>" class="text_com" value="<?=$row['quo_total'];?>" />
        </td>
        <td>
            <input type="text" name="quo_btw_<?=$j?>" class="text_com" value="<?=$row['quo_btw'];?>" />
        </td>
    </tr>
<?
    $j++;
    }
?>
    </tbody>
</table>
</div>
<br />
<!-- END OF COMPUTATION UPDATE -->
<input type="submit" name="up_item_list" value="Werk de vorm" /> - <input type="submit" name="new_save_list" value="Opslaan nieuw item" /> - <a href="">Annuleren</a>
</form>
4

2 に答える 2

1

で構築されたIDの代わりに、クラスを使用して対応する要素を見つけることjをお勧めします(コードを見てどのように動作するかわかりません)

jQuery('.new_quan').keyup(function(){        
        var new_sum = parseInt(jQuery(this).val(), 10);
        new_sum *= parseInt(jQuery(this).closest('tr').find('.new_amt').val(), 10);
        jQuery(this).closest('tr').find('.new_total').val(new_sum);
    });

そしてphpコードを更新します:

    while( $row = mysql_fetch_array( $sqlview ) ) {
    ?>
    <tr>
        <td>
            <input type="hidden" name="id_<?=$j?>" value="<?=$row['q_id'];?>" />
            <input type="hidden" name="counter" value="<?=$j?>" />
            <input type="text" name="category_<?=$j?>" class="text_com" value="<?=$row['category'];?>" />
        </td>
        <td>
            <input type="text" name="quantity_<?=$j?>" id="new_quan_<?=$j?>" class="text_com new_quan" value="<?=$row['quo_quantity'];?>" /> x
        </td>
        <td width="200">
            <input type="text" name="quo_definition_<?=$j?>" class="input" value="<?=$row['quo_definition'];?>" />
        </td>
        <td>
            <input type="text" name="quo_amt_<?=$j?>" id="new_amt_<?=$j?>" class="text_com new_amt" value="<?=$row['quo_amt'];?>" />
        </td>
        <td id="total">
            <input type="text" name="quo_total_<?=$j?>" id="new_total_<?=$j?>" class="text_com new_total" value="<?=$row['quo_total'];?>" />
        </td>
        <td>
            <input type="text" name="quo_btw_<?=$j?>" class="text_com" value="<?=$row['quo_btw'];?>" />
        </td>
    </tr>
<?
    $j++;
    }

ここでは、入力にいくつかのクラスを追加したので、find.find('.new_total').find('.new_amt')

.closest("tr")total を更新するために必要なすべての入力を含む tr が見つかります。その tr 内のみをfind('...')検索し、関連する入力値のみをその方法でのみ更新します。

于 2012-12-18T13:31:06.750 に答える
0

たぶんあなたはこれを使用$(this)してthis.id好きにする必要があります:

jQuery(document).ready(function(){
  $('input[type=text]').each(function(){
    $(this).keyup(function(){
      var j = this.id.split('_')[2];
      var new_sum = parseInt($('input[id=new_quan_'+j+']').val(), 10);
      new_sum *= parseInt($('input[id=new_amt_'+j+']').val(), 10);
      jQuery('input[id=new_total_'+j+']').val(new_sum);
    });
  });
});
于 2012-12-18T13:32:18.890 に答える