我需要删除div中的重复项我正在加载输入类型值,这些值是我从csv获取的值,并且值存储在cells[0],cells[1],cells[2]
中,但我将这些值加载到输入中
"<input type='checkbox' class ='chk' name='locationthemes' onclick='pandu();' value ="+cells[0]+"> " +cells[0]+ " </input><br>"); in that i am getting duplicates items how can i remove "
ex:<div id="checkboxes" style="display: block;">
<input type="checkbox" class="chk" name="locationthemes" onclick="pandu();" value="GreatLakes"> GreatLakes <br><input type="checkbox" class="chk" name="locationthemes" onclick="pandu();" value="GreatLakes"> GreatLakes </div>
我试过下面提到的代码,但没有工作
var seen = {};
$('#checkboxes').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
答案 0 :(得分:0)
请检查以下https://jsfiddle.net/64xvLfjm/1/
<强> HTML 强>
<div>
<input type="button" id="btnClick" value="Click" class="Button" />
</div>
<div class="chkboxs">
</div>
<强>的jQuery 强>
$(document).ready(function(){
$('#btnClick').click(function () {
var myArr = ["abc","bbb","ccc","aaa","bbb","ccc"];
var newArr = $.unique(myArr);
for (var i = 0; i < newArr.length; i++) {
$(".chkboxs").append("<input type='checkbox'>"+newArr[i]+"<br>");
}
});
});