Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
Javascript 根据表格内容创建选择选项下拉列表_Javascript_Jquery_Html Select - Fatal编程技术网

Javascript 根据表格内容创建选择选项下拉列表

Javascript 根据表格内容创建选择选项下拉列表,javascript,jquery,html-select,Javascript,Jquery,Html Select,下面是我的代码片段,它根据表中的数据创建了一个选择选项下拉列表,它工作正常,但我不想重复该选项,例如,如果已经有“medicine 1或option value medicine_1”,则不要再次添加,简言之,如果存在相同的选项,则不要追加。有什么帮助、想法、线索、建议、建议吗 $(文档).ready(函数(){ $(“按钮”)。单击(函数(){ $(“表tr”)。每个(函数(){ $(“选择”).append(“”+$(this).find(“td:nth child(2)”).text()

下面是我的代码片段,它根据表中的数据创建了一个选择选项下拉列表,它工作正常,但我不想重复该选项,例如,如果已经有“medicine 1或option value medicine_1”,则不要再次添加,简言之,如果存在相同的选项,则不要追加。有什么帮助、想法、线索、建议、建议吗

$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“表tr”)。每个(函数(){
$(“选择”).append(“”+$(this).find(“td:nth child(2)”).text()+“”);
});
});
})

创造
id 1
医学1
id 1
药物2
id 1
医学1
id 1
药物2
id 1
药物3

您只需检查是否已经存在具有dupe值的选项:

$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“表tr”)。每个(函数(){
var value=$(this.find(“td:nth child(2)”).text().toLowerCase().replace(“,”);
if($(“select”).find('option[value='+value+']').length)返回;
$(“选择”).append(“”+$(this).find(“td:nth child(2)”).text()+“”);
});
});
})

创造
id 1
医学1
id 1
药物2
id 1
医学1
id 1
药物2
id 1
药物3
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
var-arr=[];
$(“表tr”)。每个(函数(){
var option=$(this.find(“td:nth child(2)”).text();
if($.inArray(option,arr)==-1){//检查id值是否不存在,然后添加它
arr.push(选项);
$(“选择”)。追加(“”+选项+“”);
}
});
});
})

创造
id 1
医学1
id 1
药物2
id 1
医学1
id 1
药物2
id 1
药物3
这里使用.unique()

$(document).ready(function(){

      $("button").click(function(){
          var items=[], options=[];

            //Iterate all td's in second column
            $('td:nth-child(2)').each( function(){
               //add item to array
               items.push( $(this).text() );       
            });

            //restrict array to unique items
            var items = $.unique( items );

            //iterate unique array and build array of select options
            $.each( items, function(i, item){
                options.push('<option value="' + item.toLowerCase().replace(" ", "_") + '">' + item + '</option>');
            });

            //finally empty the select and append the items from the array
            $('select').empty().append( options.join() );

      });
});
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
var项目=[],选项=[];
//迭代第二列中的所有td
$('td:nth child(2)')。每个(函数(){
//将项添加到数组
items.push($(this.text());
});
//将数组限制为唯一项
变量项目=$.unique(项目);
//迭代唯一数组并生成select选项的数组
$。每个(项目,功能(i,项目){
选项。推送(“”+项目+“”);
});
//最后清空数组,从数组中选择并附加项
$('select').empty().append(options.join());
});
});

您只需添加一个函数来检查下拉列表中是否已经存在此值:

var optionExists = function optionExists(select, value) {
  var found = false;
  select.find('option').each(function(option) {
    if ($(this).val() === value && !found) {
      found = true;
    }

  });
  return found;
}
这很简单,在添加新选项之前,您只需通过下拉列表中的所有现有选项来检查值

$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“表tr”)。每个(函数(){
var value=$(this.find(“td:nth child(2)”).text().toLowerCase().replace(“,”);
如果(!optionExists($(“选择”),值)){
$(“选择”).append(“”+$(this).find(“td:nth child(2)”).text()+“”);
}
});
});
})
var optionExists=函数optionExists(选择,值){
var=false;
select.find('option')。每个(函数(选项){
如果($(this).val()==找到值&&&!){
发现=真;
}
});
发现退货;
}

创造
id 1
医学1
id 1
药物2
id 1
医学1
id 1
药物2
id 1
药物3

看起来,添加了一个既没有文本也没有值的选项。有什么想法吗?请在附加选项标签时关闭选项标签,如。勾选这把小提琴这是你添加选项的方式我改变了你添加选项的方式现在可以了@CodeDemon@balachandar我没有注意到。我将把它添加到updateseems中,就像有一个带有空文本或值的附加选项一样。有什么想法吗?是的,这是固定的,您的代码同时附加了两个选项,缺少结束标记