2017-09-08 8 views
1

私のアイテムの数量を更新するために必要な2つのパラメータを持つajax関数があります。今私は、$product_idを私のajaxパラメータに渡す方法に問題があります。私はどのように私は$product_id私のajax関数を供給するために渡すことができますどのような技術がありません。 ところで私はlaravelを使っています。ここでidを取得してajaxに渡す方法

私のコードです:私の見解で、私はここでinput type="number"

をクリックしたときに私のアイテムの数量を更新したい

@foreach($products as $product) 
<tr> 
    <td class="col-sm-8 col-md-4"> 
    <div class="media"> 
     <a class="thumbnail pull-left" href="#"> <img class="media-object" src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-2/72/product-icon.png" style="width: 72px; height: 72px;"> </a> 
     <div class="media-body"> 
      <h4 class="media-heading"><a href="#">&nbsp; {{ $product['item']->product_name }}</a></h4> 
      <!-- <span>Status: </span><span class="text-success">&nbsp;<strong>In Stock</strong></span> --> 
     </div> 
    </div></td> 
    <td class="col-sm-1 col-md-1" style="text-align: center"> 
    <input type="number" class="form-control" id="productqty" max="{{ $product['maxqty'] }}" min="1" value="{{ $product['qty'] }}"> 
    </td> 
</tr> 
@endforeach 

今の私のjavascriptです:

$("#productqty").bind('keyup mouseup', function() { 
    var value = $(this).val(); 
    updateQty(?,value); 
}); 

ここに私のJavaScriptがあります。今私は選択された入力タイプ番号のproduct_idを取得したいと思います。私はそれに多くの製品を持っています。 $productqtyというIDを持つ入力タイプのkeyup mouseupイベントをクリックすると、正確なproduct_idを取得できます。そのため、私はajax関数のパラメータを入力して疑問符を付けます。 updateQty(?,value);?私はproduct_idが交換されたい場所です。あなたの必要性のための簡単な方法がために、入力属性にプロダクトIDを置くことです$(".productqty").bind(..)

<input type="number" class="form-control productqty" id="" max="{{ $product['maxqty'] }}" min="1" value="{{ $product['qty'] }}"> 

:idは唯一のDOM、それのための使用クラスを処理する使用してイベントハンドラの

答えて

0

、今のIDは、私たちが好きでそれを使用することができ、自由に使用できます:

<input type="number" class="form-control productqty" id="{{ $product['id'] }}" max="{{ $product['maxqty'] }}" min="1" value="{{ $product['qty'] }}"> 

、あなたはそれが(jquery.attr使用して得ることができます)、$(this).attr('id');

$(".productqty").bind('keyup mouseup', function() { 
    var product_id = $(this).attr('id'); 
    var value = $(this).val(); 
    updateQty(product_id, value); 
}); 
+0

あなたが私に与えたテクニックに感謝します。私はこれを試してみます。 –

関連する問題