【问题标题】:Check Items Already exista in the Table using Jquery使用Jquery检查表中已经存在的项目
【发布时间】:2014-09-09 06:19:30
【问题描述】:

我有一个 Select2 列表并向表中添加了多个项目。但我想首先检查表中是否已经存在项目,如果不仅然后添加新项目但下面我的代码不起作用。

在这段代码中每次显示 Not Found 并添加重复行

HTML 部分

<select id="select-product" multiple style="width: 300px">
</select>

代码

  $("#select-product").change(function () {
        debugger;
        var $option = $('#select-product option');
        if ($("#select-product option[value='ProductCode']").length > 0) {
            alert("Found");
        }
        else
        {
            alert("Not Found");
        }

        $option.each(function () {
            if ($(this).is(':selected')) {
                debugger;
                var itm = $(this).is(':selected');
                var temp = $(this).attr('ProductCode');

                var row = '<tr>';
                row += '<td class="itmcode">' + $(this).attr('ProductCode') + '</td>';
                row += '<td class="itmname">' + $(this).text().replace(temp, " ") + '</td>';
                row += '<td class="unit">' + $(this).attr('Unit_UnitId') + '</td>';
                row += '<td class="retprice" dir="rtl" align="right">' + $(this).attr('RetailPrice') + '</td>';
                row += '<td class="col-md-1 inpqty" dir="rtl">' + '<input type="text" class="input-qty form-control col-md-3 center-block input-sm" data-id="' + $(this).val() + '" data-prod-id="' + $(this).attr('Value') + '">' + '</td>';
                row += '<td class="col-md-1 disc" dir="rtl">' + '<input type="text" class="input-disc form-control input-sm">' + '</td>';
                row += '<td class="tot" dir="rtl" align="right">0</td>';
                row += '<td class="imgdel"><img class="btn-img-del" src="../Images/delete.png" alt="" title="Delete" /></td>';
                row += '</tr>';

                table.append(row);
            }

        });


        $('#select-product').select2('data', null);



    });

多个产品代码

 function CallMultipleProducts() {

        $.ajax({
            type: "POST",
            url: '/Sales/GetMultipleProducts',
            contentType: "application/; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (msg) {
                debugger;
                if (msg._productlist.length > 0) {
                    debugger;
                    $.each(msg._productlist, function (index, item) {
                        debugger;
                        $('#select-product').append($("<option></option>")
                            .attr("Value", item.ProductId)
                            .attr("RetailPrice", item.RetailPrice)
                            .attr("ProductCode", item.ProductCode)
                            .text((item.ProductCode) + " " + (item.ProductName))
                            );
                    });
                }
            }
            // error: AjaxError
        })
    }

【问题讨论】:

  • 您能否提供声明您的 select2 选项的代码。
  • @VaibhavKatole 添加
  • 你想限制用户添加已经在select2列表中的重复产品吗?

标签: c# javascript jquery asp.net


【解决方案1】:

确保在整个 html 中只有 select 具有唯一 id="select-product" 的项目。

试试下面的代码,使用.find()

if ($("#select-product").find("option[value='ProductCode']").length > 0) {
     alert("Found");
}

【讨论】:

  • 您确定没有多个select 框与相同的id 吗?可以发个 jsifddle 链接吗?
【解决方案2】:

如果元素为 input type="text",请尝试将以下选项添加到您的 select2 选项中,

$("#select-product").select2({
    createSearchChoice:function(term, data) {  if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} }
});

如果你使用&lt;select&gt;元素,恐怕你不能使用这个选项。您必须改用input。你可能会发现一些有用的东西here

【讨论】:

  • 错误:当附加到
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
相关资源
最近更新 更多