Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果存在';jQuery中有一个重复的条目_Jquery_Laravel - Fatal编程技术网

如果存在';jQuery中有一个重复的条目

如果存在';jQuery中有一个重复的条目,jquery,laravel,Jquery,Laravel,嗨,我正在学习编写jQuery代码。如果条目与另一个附加行重复,如何禁用或可能执行警报 这是我的追加行脚本 var rowCount = 1; $('#add').click(function() { rowCount++; $('#orders').append('<tr id="row'+rowCount+'"><td><input class="form-control autocomplete_txt product_code" type=

嗨,我正在学习编写jQuery代码。如果条目与另一个附加行重复,如何禁用或可能执行警报

这是我的追加行脚本

var rowCount = 1;

$('#add').click(function() {
    rowCount++;
    $('#orders').append('<tr id="row'+rowCount+'"><td><input class="form-control autocomplete_txt product_code" type="text" data-type="product_code" id="product_code_'+rowCount+'" name="product_code[]" for="'+rowCount+'" required/></td><td><input class="form-control autocomplete_txt" type="text" data-type="product_name" id="product_name_'+rowCount+'" name="product_name[]" for="'+rowCount+'"required/></td><td><input class="form-control product_price" type="number" data-type="product_price" id="product_price_'+rowCount+'" name="price[]" for="'+rowCount+'" required/></td><td><input class="form-control quantity" type="number" class="quantity" id="quantity_'+rowCount+'" name="quantity[]" for="'+rowCount+'" required/><span id="quantity_warning_'+rowCount+'"></span> </td><td><input class="form-control total_price" type="text" id="total_price_'+rowCount+'" name="total_price[]"  for="'+rowCount+'" readonly required/> <input class="form-control" type="hidden" data-type="product_id" id="product_id_'+rowCount+'" name="product_id[]"/><input class="form-control" type="hidden" data-type="order_id" id="order_id_'+rowCount+'" name="order_id[]" value="1"/></td><td><button type="button" name="remove" id="'+rowCount+'" class="btn btn-danger btn_remove cicle"><i class="fas fa-times-circle"></i></button></td></tr>');
});

提前非常感谢

您可以使用一个普通的老JavaScript数组来实现这一点。添加行时,使用以下逻辑检查所需触发器上是否存在重复项。如果没有重复项,并且可以使用产品代码添加产品,则可以将其添加到
productCodes
数组中。如果需要通过页面加载使其持久化,可以通过将其保存到
local
session
存储中来利用
Web存储

// initially defined array to hold the product codes (can be toward the top of your javascript where constants and other top level vars are declared)
var productCodes = [];

/* 
    - when the trigger you're using is triggered to add the product code
        - you get the product code associated with what you've added
        - you call the isDuplicateProductCode function with that product code
    
*/

function isDuplicateProductCode(code) {
    // apply any other checking logic you need here

    // jQuery array method to check if the code passed to the function is in the productCodes array
    return productCodes.includes(code);
}
如上述功能所示,如果是重复的产品代码,则可以应用任何逻辑,例如:显示警报、弹出模式、显示必须使用不同代码的错误/警告文本

更新: 如果您已经在迭代产品代码并将其添加到数组中,那么您应该能够添加上述逻辑以检查是否存在任何重复项,如果存在,则不要运行追加逻辑。如果没有重复项,继续正常工作

更新2: 您可以将此逻辑添加到循环下面,以检查是否存在重复项,如果没有,则仅追加项

select: function( event, ui ) {
    var data = ui.item.data;
    var arr = []; 
    id_arr = $(this).attr('id');
    id = id_arr.split("_");
    elementId = id[id.length-1];

    $('.product_code').val(data.product_code).each(function() {
      arr.push($(this).val());
    });
    
    // added logic to check if there are duplicates in the array we just populated with product codes, checked against the passed in product code 
    if(arr.includes(data.product_code) {
        // at this point, this is a duplicate product code and you need to display your warning/alert/modal/text (whatever type you want)
    } else {
        // this logic is not executed because the product code is a duplicate
        $('#product_code_'+elementId).val(data.product_code);
        $('#product_name_'+elementId).val(data.name);
        $('#product_price_'+elementId).val(data.price).prop('min', data.price);
        $('#product_id_'+elementId).val(data.id);
        $('#quantity_'+elementId).prop('max', data.quantity);
        $('#quantity_warning_'+elementId).text('You have '+data.quantity+' in your stocks');
    }
}

你会称之为复制品吗?当给定两行的所有表格单元格数据都相同时?Hi@HaiderAli产品代码。你能粘贴你的表格html以便我可以构造jquery代码段来实现它吗?Hi@HaiderAli我刚刚更新了我的问题。谢谢谢谢@ChrisHennighan。不幸的是,我刚刚开始学习编写javascript/jQuery代码,您能指导我完成吗?我应该把$('#add')放进去。单击(函数(){rowCount++;$('#transfer')。append(-);});在var productCodes=[]之间;函数是duplicateProductCode(code){?我应该在(code)中放什么?我在这里也使用了laravelproject@Lito有一些事情要考虑。要确保你能对所有来自服务器的产品代码进行核算(不确定你是否从服务器获得了值,或者只是添加新的代码)。。如果您只是添加新的产品代码,那么您只需向顶部声明数组(当您的js文件加载时)。
productCodes.includes(代码)
是一种jQuery数组方法,用于检查代码是否在数组中。如果这是您所需要的全部逻辑,则可以保持原样。@Lito我已经用一些更详细的注释更新了初始答案。在调用
$('.product_code').val(data.product_code)时,您似乎已经在迭代每个产品代码。每个(函数(){arr.push($(this.val());});
,所以在开始设置循环下面的属性之前,您可以添加逻辑来检查重复项。它很有效!!!非常感谢@ChrisHennighan!现在我只需要修改它的html部分,但最重要的是现在表单可以识别重复项。。再次感谢!
    select: function( event, ui ) {
      var data = ui.item.data; 
      var arr = []; 

      id_arr = $(this).attr('id');
      id = id_arr.split("_");
      elementId = id[id.length-1];

      $('.product_code').each(function() {
        arr.push($(this).val());
      });

      // added logic to check if there are duplicates in the array we just populated with product codes, checked against the passed in product code 
      if(arr.includes(data.product_code)) {
          $('#quantity_warning_'+elementId).text('duplicated');
      } else {
        $('#product_code_'+elementId).val(data.product_code);
        $('#product_name_'+elementId).val(data.name);
        $('#product_price_'+elementId).val(data.price).prop('min', data.price);
        $('#product_id_'+elementId).val(data.id);
        $('#quantity_'+elementId).prop('max', data.quantity);
        $('#quantity_warning_'+elementId).text('You have '+data.quantity+' in your stocks');
      }
    }
// initially defined array to hold the product codes (can be toward the top of your javascript where constants and other top level vars are declared)
var productCodes = [];

/* 
    - when the trigger you're using is triggered to add the product code
        - you get the product code associated with what you've added
        - you call the isDuplicateProductCode function with that product code
    
*/

function isDuplicateProductCode(code) {
    // apply any other checking logic you need here

    // jQuery array method to check if the code passed to the function is in the productCodes array
    return productCodes.includes(code);
}
select: function( event, ui ) {
    var data = ui.item.data;
    var arr = []; 
    id_arr = $(this).attr('id');
    id = id_arr.split("_");
    elementId = id[id.length-1];

    $('.product_code').val(data.product_code).each(function() {
      arr.push($(this).val());
    });
    
    // added logic to check if there are duplicates in the array we just populated with product codes, checked against the passed in product code 
    if(arr.includes(data.product_code) {
        // at this point, this is a duplicate product code and you need to display your warning/alert/modal/text (whatever type you want)
    } else {
        // this logic is not executed because the product code is a duplicate
        $('#product_code_'+elementId).val(data.product_code);
        $('#product_name_'+elementId).val(data.name);
        $('#product_price_'+elementId).val(data.price).prop('min', data.price);
        $('#product_id_'+elementId).val(data.id);
        $('#quantity_'+elementId).prop('max', data.quantity);
        $('#quantity_warning_'+elementId).text('You have '+data.quantity+' in your stocks');
    }
}