检查字符串中的变量

时间:2017-09-21 01:01:52

标签: jquery arrays string

我有一串名字除以|

| akong | adelyn | ahchong |彼得|约翰

然后我想插入新名称ahchong如何检查字符串中是否已存在该名称并且不允许插入它?

我尝试了什么。我有一个字符串:

var listOfElements = $(this).closest(".procLeader").find("ol").text();
var trim_listOfElements = listOfElements.replace(/ /g, '');

当前名称是可拖动元素:

var current_name = ui.draggable.text();
var trim_current_name = $.trim(current_name);
var substr_current_name = (trim_current_name).substr(2);

我尝试indexOf()检查名称是否在字符串中:

if (trim_listOfElements.indexOf(substr_current_name) >= 0){
  $('.test2').html('IN STRING');
} else {
  $('.test2').html('NOT IN STRING');
}

我在drop函数里面做了一切,这里是完整的函数:

$(function(){
  var itm = [];  
  $(".procLeader ol").droppable({
    tolerance: 'pointer', 
    hoverClass: 'highlight',
    drop: function(ev, ui){
      var current_name = ui.draggable.text();
      var new_array_name = itm.includes(current_name);
      if (new_array_name === false) {
          $(this).find(".placeholder").empty();//remove text inside placeholder
          $(this).find(".placeholder").css('height', '20px');//change height of placeholder class
          var item = ui.draggable;
        if (!ui.draggable.closest('.placeholder').length){
          item = item.clone().draggable();// if item was dragged from the source list - clone it
          item.addClass('droppedItem').appendTo(this);// append item to placeholder
          $(".droppedItem").draggable({ disabled: true });

          var listOfElements = $(this).closest(".procLeader").find("ol").text();
          var trim_listOfElements = listOfElements.replace(/ /g, '');

          var trim_current_name = $.trim(current_name);
          var substr_current_name = (trim_current_name).substr(2);

          $(".test").val(trim_listOfElements);
          if (trim_listOfElements.indexOf(substr_current_name) >= 0){
            $('.test2').html('IN STRING');
          } else {
            $('.test2').html('NOT IN STRING');
          }   
        }        
      }  
    }
  });
});

如何检查字符串中是否有变量而不将其添加到那里?

修改

这是我尝试过的。我将字符串更改为数组:

      var newarr =  trim_listOfElements.split('|');
      newarr.shift();//remove fisrt empty element

并尝试使用inArray()检查:

      if($.inArray(substr_current_name, newarr) !== -1) {
        $('.test2').html('IN ARRAY');
      } else {
        $('.test2').html('NOT IN ARRAY');
      }

现在全功能:

$(".procLeader ol").droppable({
  tolerance: 'pointer', 
  hoverClass: 'highlight',
  drop: function(ev, ui){
    var current_name = ui.draggable.text();
    var new_array_name = itm.includes(current_name);
    if (new_array_name === false) {
        $(this).find(".placeholder").empty();//remove text inside placeholder
        $(this).find(".placeholder").css('height', '20px');//change height of placeholder class
        var item = ui.draggable;
      if (!ui.draggable.closest('.placeholder').length){
        item = item.clone().draggable();// if item was dragged from the source list - clone it
        item.addClass('droppedItem').appendTo(this);// append item to placeholder
        $(".droppedItem").draggable({ disabled: true });

        var listOfElements = $(this).closest(".procLeader").find("ol").text();
        var trim_listOfElements = listOfElements.replace(/ /g, '');

        var trim_current_name = $.trim(current_name);
        var substr_current_name = (trim_current_name).substr(2);

        var newarr =  trim_listOfElements.split('|');
        newarr.shift();//remove fisrt empty element

        if($.inArray(substr_current_name, newarr) !== -1) {
          $('.test2').html('IN ARRAY');
        } else {
          $('.test2').html('NOT IN ARRAY');
        }
      }      
    }  
  }
});

现在它为我删除的每个名字显示NOT IN ARRAY

1 个答案:

答案 0 :(得分:0)

不要使用indexOf,我已经看到它以这种方式使用,返回误报并导致错误。

改为使用

$.inArray(substr_current_name, trim_listOfElements.split('|'))