【问题标题】:From value and key , How to fetch the key and do the multiplication of price and Quantity?从 value 和 key ,如何获取 key 并做 price 和 Quantity 的乘法?
【发布时间】:2020-04-07 12:57:11
【问题描述】:

当我选择类别时,它会在选择框上显示价格,但是当我将值放入数量框中时,它是价格值和数量的乘积并显示总数,但是当总数到达时,它是获取价格和数量的id乘以id.. 我想要数量和价格的乘积而不是价格的价值..那我怎么能???

用于从子类别中选择价格值的 JavaScript

<script src="{{ asset('js/jquery.min.js') }}"></script>
<script type="text/javascript">
    // now change price..........
     $(document).ready(function() 
     {    
         $('select#subcategory').on('change', function() {
            if(subID = $(this).val()) 
            {
                $.ajax({
                        url: "{{url('/')}}" + '/Purchaser/add_purchaser/ajax/' + subID,
                        type: "GET",
                        dataType: "json",
                        success: function(data)
                        {
                            console.log(data);
                             $('select#productprice').empty();
                             $.each(data, function(key, value)
                             {
                                   var option = new Option(value, key);
                                   $(option).html(value);
                                   // $(option).html(value);
                                   $('select#productprice').append(option);
                             });
                        }
                    });
            }else{
                $('select#productprice').empty();
            }
         });
    }); 
    </script>

获取总数的javascript

<script type="text/javascript">
$('tbody').delegate('#price,#quantity','keyup',function(){
  var tr = $(this).parent().parent();
  var price = tr.find('#productprice').val();
  var quantity = tr.find('#quantity').val();
  var total = (price * quantity);
  tr.find('#total').val(total);
});
</script>

HTML 文件

<tbody>
   <tr>
      <td>
         <select class="form-control" name="cat_id[]" id="category">
             <option value="" disabled selected>Select Category</option>
                @foreach($category as $key)
                    <option value="{{ $key->id }}">{{ $key->cat_nm }}</option>
                @endforeach
         </select>
      </td>
      <td>
        <select class="form-control" id="subcategory" name="sub_cat_id[]">
          <option value="" disabled selected>Subcategory from category</option>
        </select>
     </td>
     <td>
       <select class="form-control" id="productprice" name="price_id[]">  
           <option>Price from subcategory</option>                                     
        </select>
     </td>
     <td>
        <input type="text" class="form-control" name="quantity[]" id="quantity">
      </td>
      <td>
         <input type="text" class="form-control" name="total[]" id="total">
       </td>
       <td>
          <a href="#" class="btn btn-danger" id="remove"><i class="fa fa-remove"></i></a>
        </td>
        </tr>
</tbody>

【问题讨论】:

    标签: javascript ajax laravel-6


    【解决方案1】:

    使用 var price = $( "#productprice option:selected" ).text(); var price = tr.find('#productprice').val();就像下面的脚本......

     <script type="text/javascript">
          $('tbody').delegate('#price,#quantity','keyup',function(){
            var tr = $(this).parent().parent();
            var price = $( "#productprice option:selected" ).text();
            // var price = tr.find('#productprice').val();
            var quantity = tr.find('#quantity').val();
            var total = (price * quantity);
            tr.find('#total').val(total);
          });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2017-09-12
      • 2016-07-20
      • 2016-09-17
      相关资源
      最近更新 更多