删除' div'中的重复项在输入标签内

时间:2016-03-09 04:57:03

标签: javascript jquery html

我需要删除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;
        });

1 个答案:

答案 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>");
        }

      });
});