使用 jQuery 过滤和删除没有数据的输入

Using jQuery to filter and remove input with no data

本文关键字:数据 输入 删除 jQuery 过滤 使用      更新时间:2023-09-26

我有一个销售订单的表单。该窗体显示可以选择的所有可用产品。每行必须输入QTY和ID。 该 ID 处于隐藏状态。

我能够过滤数量输入,因为任何未选择的项目都没有值。我遇到的问题是,即使数量中没有值,ID也会有一个值。

我想做的是完全删除此行,因此如果 qty 值没有值,也删除 ID

  • 数量输入的名称是行项目[i][qty]//i 为每一行编制索引
  • id 的名称是 lineItem[i][product]//i 为每一行编制索引

JavaScript:

// This is just the function to show you how I have the input structured
var showProduct = function (result) {
    //console.log(result.id  + " " + result.product );
    var last = $('.display:last');
    $(".display ").append("<tr> <td class = '"hide'">" + result.id + " </td> <td> " + result.productName + " </td> <td >"
    + "<input type = '"number'" min ='"0'" class = '"qty'" >   
    </input> </td> <td class = '"hide'"> " + "<input class = '"id'"  value = '"" + result.productName +
        "'"></td> </tr>"
    );
    var input = $(last.find('input.qty')[i]);
    input.attr("name", "lineItem[" + i + "][qty]");
    var input2 = $(last.find('input.id')[i]);
    input2.attr("name", "lineItem[" + i + "][product]");
    //console.log(i);
    i++
};

我目前必须过滤和修剪表单结果的代码如下:

$('.form2').on ('click tap', '#submit2', (function () {
    $('form').submit (function () {
   event.preventDefault();

    var formData =  $(this).find(":input").filter(function () {
      console.log("This is the this.value" + this);
     return $.trim(this.value).length > 0
}).serializeArray();

感谢您提供的任何帮助。

您将在

事件处理程序中附加事件侦听器。我不确定这是否是有意的。我认为您要完成的是:

$('form').submit(function() {
  // filter out each "tr" that does not have a qty value
  $('.display tr').filter(function() {
    return !$(this).find('input.qty').val();
  }).remove();
});