如果元素不存在,请在javascript中添加到输入字段

时间:2014-07-30 21:20:09

标签: javascript jquery clone

我正在尝试向输入字段添加主题标签。问题是当我添加它时,我可以继续添加它。我想知道一种检查输入字段的方法,如果hashtag已经存在,不添加它?

我的javascript是:

$('.item').click(function(){

  var clonedTag = $("<div class='tag_clone' data-value=" + ($(this).attr('data-value')) + ">"+($(this).attr('data-value')) + "<a href='javascript:void(0)' class='remove' tabindex='-1' title='Remove'>" + "×" + "</a>" + "</div>")

  $('.selectize-input.items.not-full').append(clonedTag)

})

$('.selectize-input.items.not-full').on('click', '.remove', function(e){
  $(this).parent('.tag_clone').remove()
  e.preventDefault();
})

因此,如果#example1存在于顶部输入字段中,则用户无法再次添加它(我正在使用Selectize.js)

在下图中,我点击底部输入字段,将主题标签克隆到顶部输入字段。

enter image description here

0 个答案:

没有答案