Javascript 如何检查html表在js和sum输入中是否有重复的行,而不是插入另一行?

Javascript 如何检查html表在js和sum输入中是否有重复的行,而不是插入另一行?,javascript,html,jquery,ajax,duplicates,Javascript,Html,Jquery,Ajax,Duplicates,我在做POS系统的时候学习JS,我很难弄清楚如何在插入前检查添加的产品是否已经扫描,如果已经扫描,则改为更改数量输入 到目前为止,当我扫描产品id时,它插入没有问题,但当我扫描相同的id时,它插入到新行中。看来我的compobacion功能不起作用了。我尝试使用for搜索其他行,我尝试了一些在线找到的解决方案,但似乎没有任何效果 下面是一个例子,说明它正在发生什么 idProductos是主键,隐藏在行中,因此我引入了codigo(它是另一个唯一的列,两者都不能为null) 有人能帮我吗?我迷

我在做POS系统的时候学习JS,我很难弄清楚如何在插入前检查添加的产品是否已经扫描,如果已经扫描,则改为更改数量输入

到目前为止,当我扫描产品id时,它插入没有问题,但当我扫描相同的id时,它插入到新行中。看来我的compobacion功能不起作用了。我尝试使用for搜索其他行,我尝试了一些在线找到的解决方案,但似乎没有任何效果

下面是一个例子,说明它正在发生什么

idProductos是主键,隐藏在行中,因此我引入了codigo(它是另一个唯一的列,两者都不能为null)

有人能帮我吗?我迷路了

这是我的密码

    $.ajax({
        method: "POST",
        url: "../php/venta.php",
        data: param,
        success: function(data) {
            if (data != null) {

                var idProductos,
                    Codigo,
                    nombre,
                    precioVenta;

                // console.log(data);

                var rows = jQuery.parseJSON(data);

                idProductos = rows[0].idProductos;
                Codigo = rows[0].Codigo;
                nombre = rows[0].nombre;
                precioVenta = rows[0].precioVenta;

                (idProductos)

                if (comprobacion(idProductos) == false) {

                    var nuevoValor = $(parseInt($('.inputCantidad')[i]).val()) + 1;

                    $($('.inputCantidad')[i]).val(nuevoValor);

                    var valorImporte = $($('.inputprecioVenta')[i]).val() * nuevoValor;

                    $($('.inputImporte')[i]).val(valorImporte);

                } else {

                    var table = document.getElementById('tablaVenta');

                    var newRow = document.createElement("tr");
                    newRow.align = "center";

                    var contentRow =
                        '<td><input type="hidden" class="inputId" value="' + idProductos + '">' + Codigo + '</td>' +
                        '<td>' + nombre + '</td>' +
                        '<td><input class="inputprecioVenta" value="' + precioVenta + '"></td>' +
                        '<td><input class="inputCantidad" value="1"></td>' +
                        '<td><input class="inputImporte" value="' + precioVenta + '"></td>';

                    newRow.innerHTML = contentRow;


                    table.appendChild(newRow);

                }

            }
        },
        error: function(jqXHR, textStatus, errorThrown) { //errores
            alert(jqXHR + textStatus + errorThrown);

        },

    })

}

}

将id设置为HTML输入,使用JS可以更快地找到ProductID

'<td><input type="hidden" id="hid_' + idProductos + '" class="inputId" value="' + idProductos + '">' + Codigo + '</td>' +
'<td>' + nombre + '</td>' +
'<td><input id="hid_' + idProductos + '" class="inputprecioVenta" value="' + precioVenta + '"></td>' +
'<td><input id="qty_' + idProductos + '" class="inputCantidad" value="1"></td>' +
'<td><input id="cst_' + idProductos + '" class="inputImporte" value="' + precioVenta + '"></td>';
“”+Codigo+“”+
“+nombre+”+
'' +
'' +
'';
试试
$('tbody tr td')。每个(函数()


值在td中,而不是tr中。我将使用自定义数据属性(如
数据id
)将id添加到行中,并使用该属性以及一些智能的选择器创建来快速识别以前是否使用过id

$.ajax({
    method: "POST",
    url: "../php/venta.php",
    data: param,
    success: function(data) {
        if (data != null) {

            var idProductos,
                Codigo,
                nombre,
                precioVenta;

            // console.log(data);

            var rows = jQuery.parseJSON(data);

            idProductos = rows[0].idProductos;
            Codigo = rows[0].Codigo;
            nombre = rows[0].nombre;
            precioVenta = rows[0].precioVenta;

            (idProductos)

            if (comprobacion(idProductos) == false) {

                var nuevoValor = $(parseInt($('.inputCantidad')[i]).val()) + 1;

                $($('.inputCantidad')[i]).val(nuevoValor);

                var valorImporte = $($('.inputprecioVenta')[i]).val() * nuevoValor;

                $($('.inputImporte')[i]).val(valorImporte);

            } else {

                var table = document.getElementById('tablaVenta');

                var newRow = document.createElement("tr");
                newRow.align = "center";
/* Add the line below */
                newRow.setAttribute("data-id", idProductos);

                var contentRow =
                    '<td><input type="hidden" class="inputId" value="' + idProductos + '">' + Codigo + '</td>' +
                    '<td>' + nombre + '</td>' +
                    '<td><input class="inputprecioVenta" value="' + precioVenta + '"></td>' +
                    '<td><input class="inputCantidad" value="1"></td>' +
                    '<td><input class="inputImporte" value="' + precioVenta + '"></td>';

                newRow.innerHTML = contentRow;


                table.appendChild(newRow);

            }

        }
    },
    error: function(jqXHR, textStatus, errorThrown) { //errores
        alert(jqXHR + textStatus + errorThrown);

    },

})

欢迎来到StackOverflow!请拿起(并在使用时赢取一枚徽章)/也请阅读我们的页面和您的问题以改进它。好的问题往往会从社区中得到更快、更好的答案。对于初学者,请在您的问题中添加一个答案。否则,可能需要更多的时间和大量的猜测才能准确地找出问题所在。
$(此)
comprobacion
中的
指的是一个
tr
元素,该元素没有可通过
val()获取的值
。从传递给
每个
的函数返回false只会导致
每个
停止迭代,它不会从周围的函数返回false。
td
元素也没有
属性。只有
输入
选择
按钮
,和
文本区域
>有价值谢洛,谢谢你的回复。我没有注意到。遗憾的是仍然不起作用。是的,看起来太快了。使用$(“.inputId”)作为选择者,您可以选择您的答案以使其正确…否则,它不会正确回答问题谢谢您的时间,我只是尝试了您给我的调整。仍然是相同的问题,它仍然添加了重复项。然后
idProductos
不是唯一的。我只是检查了一下,idProductos是唯一的,主键可能不存在sunderstood设置了
comprobacion
应该返回的内容;如果表中不存在id,则此代码返回true。如果id不存在,则返回true。
$.ajax({
    method: "POST",
    url: "../php/venta.php",
    data: param,
    success: function(data) {
        if (data != null) {

            var idProductos,
                Codigo,
                nombre,
                precioVenta;

            // console.log(data);

            var rows = jQuery.parseJSON(data);

            idProductos = rows[0].idProductos;
            Codigo = rows[0].Codigo;
            nombre = rows[0].nombre;
            precioVenta = rows[0].precioVenta;

            (idProductos)

            if (comprobacion(idProductos) == false) {

                var nuevoValor = $(parseInt($('.inputCantidad')[i]).val()) + 1;

                $($('.inputCantidad')[i]).val(nuevoValor);

                var valorImporte = $($('.inputprecioVenta')[i]).val() * nuevoValor;

                $($('.inputImporte')[i]).val(valorImporte);

            } else {

                var table = document.getElementById('tablaVenta');

                var newRow = document.createElement("tr");
                newRow.align = "center";
/* Add the line below */
                newRow.setAttribute("data-id", idProductos);

                var contentRow =
                    '<td><input type="hidden" class="inputId" value="' + idProductos + '">' + Codigo + '</td>' +
                    '<td>' + nombre + '</td>' +
                    '<td><input class="inputprecioVenta" value="' + precioVenta + '"></td>' +
                    '<td><input class="inputCantidad" value="1"></td>' +
                    '<td><input class="inputImporte" value="' + precioVenta + '"></td>';

                newRow.innerHTML = contentRow;


                table.appendChild(newRow);

            }

        }
    },
    error: function(jqXHR, textStatus, errorThrown) { //errores
        alert(jqXHR + textStatus + errorThrown);

    },

})
function comprobacion(idProductos) {
  return $('tbody tr[data-id="' + idProductos + '"]').length === 0;
}