【问题标题】:how to get each value from span and set all into variables using js or jquery? [closed]如何从 span 中获取每个值并使用 js 或 jquery 将所有值设置为变量? [关闭]
【发布时间】:2021-02-07 22:28:55
【问题描述】:

我想在添加任何单词之前将标签列表检查到 div 中以不通过 jquery 重复任何单词

$('.addTag').on('keyup',function(e){
var inputTag = e.keyCode || e.which;
var spanValues = document.getElementsByClassName('mini-tag');
var thisValue = $(this).val().slice(0,-1);
console.log(spanValues)
if(inputTag === 188){
  for(i=0;i<spanValues.length;i++){
    if(thisValue === spanValues[i]){
      $('.addTag').val('')
    }else{
      $('.tags').append("<span class='mini-tag'>" +  thisValue + 
       "<i class='fas fa-times fa-xs'></i>" + " "  + "</span>");
      $(this).val('');} 
    }
  }
})

【问题讨论】:

  • 请详细说明您要达到的目标是什么?
  • 并发布所需的输出
  • 在网站中添加任何标签以不重复标签中的单词之前检查列表
  • 您能否显示相关的 HTML,以便我们重现您的问题?请阅读“minimal reproducible example”和“How to Ask”指南,如果您可以将相关代码发布为“Stack Snippet”,将不胜感激。

标签: javascript jquery for-loop duplicates


【解决方案1】:

每次用户释放一个键时,您都会查看它,然后如果它是终止字符(您选择了小于号/左尖括号),您会得到他们输入的整个字符串,减去终止字符。

到目前为止一切顺利。

事情有点出问题的地方在于你的循环。您正在逐步检查每个现有的 mini-tag span 元素,但您正在根据用户输入的值测试整个元素。您需要仅针对第一个文本节点进行测试,否则您将永远无法获得匹配。

另外,当没有迷你标签元素时,一开始会发生什么?长度将为 0,因此您永远不会进入循环 - 您永远不会开始。

我们可以做的是记住我们是否有匹配,当你有匹配时打破你的循环,然后如果我们没有匹配,则附加迷你标签跨度。

看看这个sn-p和代码中的cmets。

顺便说一句,需要注意的一件事是,如果您的用户在您的输入元素中输入了一些讨厌的代码,您将其放入 DOM 中,这可能很危险。由于您在谈论标签,因此您可能希望丢弃任何不是字母数字的字符,但这取决于您的用例。

$('.addTag').on('keyup',function(e){
var inputTag = e.keyCode || e.which;
if (inputTag === 188) {
var spanValues = document.getElementsByClassName('mini-tag');
var thisValue = $(this).val().slice(0,-1);
console.log(spanValues)

//We just want to see if we have got a match
//We havent got one yet so let's remember that
let gotMatch = false;

//then go through testing the latest tag input against all those we already have to see if there is a match

  for(i=0;i<spanValues.length;i++){
    if(thisValue === spanValues[i].firstChild.nodeValue){
     gotMatch = true;
     break; //once we've got a match we know we don't want to carry on looking through the existing tags so get out of the for loop
    }
  }
    
    //now we have been through all the exising tags
    //we append a span element with the tag (and some fancy fontawesome)into the tags div
    
    if (!gotMatch) {
      $('.tags').append("<span class='mini-tag'>" +  thisValue + 
       "<i class='fas fa-times fa-xs'></i>" + " "  + "</span>"); 
       
    }
    $(this).val(''); //and whether we got a match or not we want to clear the input element so the user can type in another tag
}})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="addTag" type="text"/>
<div class="tags"></div>

【讨论】:

  • 谢谢A Howorth,但是当代码运行时,显示这个错误,想显示错误但我不知道怎么做???
  • 我没有看到错误。你能告诉我它是什么吗?
  • 这里如何显示错误?? -- 如果您有电子邮件要发送错误,请发送它
  • 您能否将其复制并粘贴到评论中,或者直接输入评论的主要部分。此外,您何时收到错误,当您将终止字符键入输入或任何字符或....
  • 非常感谢您的代码完美且有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
相关资源
最近更新 更多