Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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
C# 使用Jquery检查表中已存在的项_C#_Javascript_Jquery_Asp.net - Fatal编程技术网

C# 使用Jquery检查表中已存在的项

C# 使用Jquery检查表中已存在的项,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我有一个Select2列表,并向表中添加了多个项目。但我想先检查表中是否已经存在项目,然后再添加新项目,但我的代码下面的项目是否不起作用 在这段代码中,每次都显示NotFound&添加重复行 HTML部分 <select id="select-product" multiple style="width: 300px"> </select> 代码 $(“#选择产品”)。更改(功能(){ 调试器; var$option=$(“#选择产品选项”); 如果($(“#选择产

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

在这段代码中,每次都显示NotFound&添加重复行

HTML部分

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

代码

$(“#选择产品”)。更改(功能(){
调试器;
var$option=$(“#选择产品选项”);
如果($(“#选择产品选项[value='ProductCode']”)。长度>0){
警惕(“发现”);
}
其他的
{
警报(“未找到”);
}
$option.each(函数(){
如果($(this).is(':selected')){
调试器;
var itm=$(this).is(':selected');
var temp=$(this.attr('ProductCode');
var行=“”;
行+=''+$(this.attr('ProductCode')+'';
行+=''+$(this).text().replace(temp,'')+'';
行+=''+$(this).attr('Unit_UnitId')+'';
行+=''+$(this.attr('RetailPrice')+'';
行+=''+''+'';
行+=''+''+'';
行+='0';
行+='';
行+='';
表.追加(行);
}
});
$(“#选择产品”).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
        })
    }
function CallMultipleProducts(){
$.ajax({
类型:“POST”,
url:“/Sales/GetMultipleProducts”,
contentType:“应用程序/;字符集=utf-8”,
数据类型:“json”,
async:false,
成功:功能(msg){
调试器;
如果(msg.\u productlist.length>0){
调试器;
$.each(msg.\u产品列表,功能(索引,项目){
调试器;
$(“#选择产品”)。附加($(“”)
.attr(“值”,item.ProductId)
.attr(“零售价”,商品零售价)
.attr(“ProductCode”,item.ProductCode)
.text((item.ProductCode)+“”+(item.ProductName))
);
});
}
}
//错误:AjaxError
})
}

确保整个html中只有
select
项具有唯一的
id=“select product”

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


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


如果您使用的是
元素,恐怕无法使用此选项。您必须使用
输入
。您可能会发现一些有用的东西。

您能否提供声明select2选项的代码。@VaibhavKatole ADDED您想限制用户添加select2列表中已经存在的重复产品吗?您确定没有多个id相同的
select
框吗?您可以发布jsifddle链接吗?错误:Select2在附加到元素时不允许使用选项“createSearchChoice”。
 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
        })
    }
if ($("#select-product").find("option[value='ProductCode']").length > 0) {
     alert("Found");
}
$("#select-product").select2({
    createSearchChoice:function(term, data) {  if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} }
});