获取jQuery Data并将其用作laravel中数字字段的MAX值

问题描述 投票:0回答:2

嗨,我有这个jQuery脚本,它抓取某个laravel变量并设置为数字字段的最大值。这样我就可以确保用户输入的值不能大于变量的值。这是脚本

var $limitQty = $('#product_name_1').val();
$('#product_name_1'+$orderItem_id).on('mouseup keyup', function () {
    $(this).val(Math.min({{$remainingDeliveries}}, Math.max(0, $(this).val())));
});

这是数字字段

<input class="form-control qty_in" type='text' data-type="qty_in" id='qty_in_1' name='qty_in[]' data-max='{{$remainingDeliveries}}' onkeyup='check(this);' for="1"/>

他们工作正常

现在我的问题是我有表单,但是相同的数字字段,需要这个具有不同数据源的脚本。数据来自自动完成jQuery脚本。这是代码,

//autocomplete script
$(document).on('focus','.autocomplete_txt',function(){
  type = $(this).data('type');

  if(type =='product_code' )autoType='product_code'; 
  if(type =='product_name' )autoType='name'; 
  // if(type =='qty_in' )autoType='qty_in'; 
  // if(type =='product_id' )autoType='id'; 

   $(this).autocomplete({
       minLength: 0,
       source: function( request, response ) {
            $.ajax({
                url: "{{ route('searchStock') }}",
                dataType: "json",
                data: {
                    term : request.term,
                    type : type,
                },
                success: function(data) {
                    var array = $.map(data, function (item) {
                       return {
                           label: item[autoType],
                           value: item[autoType],
                           data : item
                       }
                   });
                    response(array)
                }
            });
       },
       select: function( event, ui ) {
           var data = ui.item.data;           
           id_arr = $(this).attr('id');
           id = id_arr.split("_");
           elementId = id[id.length-1];
           $('#product_code_'+elementId).val(data.product_code);
           $('#product_name_'+elementId).val(data.name);
           $('#qty_in_'+elementId).val(data.qty_in);
           $('#qty_out_'+elementId).val(data.qty_out);
           $('#product_id_'+elementId).val(data.id);
       }
   });
});

我想要完成的是,如果可能的话,我想从$('#qty_in _'+ elementId).val(data.qty_in)获取值;把它放在$(this).val(Math.min({{--- PUT HERE ---}}, Math.max(0, $(this).val())));有可能吗?请帮帮我。非常感谢你提前!

jquery laravel
2个回答
0
投票

您可以在输入中添加数据属性,并根据自己的需要访问它

select: function( event, ui ) {
       ...
       $('#qty_in_'+elementId).val(data.qty_in).data('max',data.qty_in);
}

0
投票

这个对我有用。

select:function(event,ui){... $('#qty_in _'+ elementId).val(data.qty_in).prop('max',data.qty_in); }

© www.soinside.com 2019 - 2024. All rights reserved.