Javascript 如果某个元素的文本不';与其他元素文本不匹配?

Javascript 如果某个元素的文本不';与其他元素文本不匹配?,javascript,jquery,Javascript,Jquery,我有一个输入和一些结果,如果任何结果与其文本标题与输入的确切.val()不匹配,则应删除其.parent().parent()。我遇到的问题是,在我执行$this.parent().parent().hide()时,将错误的指向this

我有一个
输入
和一些结果,如果任何结果与其文本标题与输入的确切
.val()
不匹配,则应删除其
.parent().parent()
。我遇到的问题是,在我执行
$this.parent().parent().hide()时,将错误的
指向this
$this
var$this=jQuery(this).text().toLowerCase()

函数fetch(){
jQuery.ajax({
url:“”,
键入:“post”,
数据:{action:'data_fetch',exactwords:jQuery('#usp title').val()},
成功:功能(数据){
var text2;
var text2B;
text2=jQuery('#usp title').val();
text2B=text2.toLowerCase();
jQuery(“#datafetch”).html(data.promise().done(function()){
jQuery(“#datafetch ul li h2 a”)。每个(函数(){
var$this=jQuery(this).text().toLowerCase();
如果($this!=text2B){
$this.parent().parent().hide();
}else if(text1B==text2B){
jQuery(“#componi”).attr(“disabled”、“disabled”).hide();
}
});
});
}
});
}
只需执行
$(this.parent().parent().hide()

$this
设置为
$(this)
以外的任何值都是不好的做法

只需重命名变量并执行以下操作

function() {
  var $this = jQuery(this),
      text = $this.text().toLowerCase();
  if (text != text2B) {
    $this.parent().parent().hide();
  } else if (text1B == text2B) {
    jQuery("#componi").attr("disabled", "disabled").hide();
  }
}
只需执行
$(this.parent().parent().hide()

$this
设置为
$(this)
以外的任何值都是不好的做法

只需重命名变量并执行以下操作

function() {
  var $this = jQuery(this),
      text = $this.text().toLowerCase();
  if (text != text2B) {
    $this.parent().parent().hide();
  } else if (text1B == text2B) {
    jQuery("#componi").attr("disabled", "disabled").hide();
  }
}

问题是您正在将
$this
设置为字符串,而不是jQuery节点,因此当您说
$this.parent().parent()
时,它将不起作用。设置
$this=jQuery(this)并在条件中内联执行值查找

function fetch(){
  jQuery.ajax({
    url: '<?php echo admin_url('admin-ajax.php'); ?>',
    type: 'post',
    data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
    success: function(data) {
      var text2;
      var text2B;
      text2 = jQuery('#usp-title').val();
      text2B = text2.toLowerCase();
      jQuery("#datafetch").html(data).promise().done(function(){
        jQuery("#datafetch ul li h2 a").each(function() {
          var $this = jQuery(this);
          if ($this.text().toLowerCase() !== text2B) {
            $this.parent().parent().hide();
          } else if (text1B == text2B) {
            jQuery("#componi").attr("disabled", "disabled").hide();
          }
        });
      });
    }
  });
}
函数fetch(){
jQuery.ajax({
url:“”,
键入:“post”,
数据:{action:'data_fetch',exactwords:jQuery('#usp title').val()},
成功:功能(数据){
var text2;
var text2B;
text2=jQuery('#usp title').val();
text2B=text2.toLowerCase();
jQuery(“#datafetch”).html(data.promise().done(function()){
jQuery(“#datafetch ul li h2 a”)。每个(函数(){
var$this=jQuery(this);
if($this.text().toLowerCase()!==text2B){
$this.parent().parent().hide();
}else if(text1B==text2B){
jQuery(“#componi”).attr(“disabled”、“disabled”).hide();
}
});
});
}
});
}

问题是您正在将
$this
设置为字符串,而不是jQuery节点,因此当您说
$this.parent().parent()
时,它将不起作用。设置
$this=jQuery(this)并在条件中内联执行值查找

function fetch(){
  jQuery.ajax({
    url: '<?php echo admin_url('admin-ajax.php'); ?>',
    type: 'post',
    data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
    success: function(data) {
      var text2;
      var text2B;
      text2 = jQuery('#usp-title').val();
      text2B = text2.toLowerCase();
      jQuery("#datafetch").html(data).promise().done(function(){
        jQuery("#datafetch ul li h2 a").each(function() {
          var $this = jQuery(this);
          if ($this.text().toLowerCase() !== text2B) {
            $this.parent().parent().hide();
          } else if (text1B == text2B) {
            jQuery("#componi").attr("disabled", "disabled").hide();
          }
        });
      });
    }
  });
}
函数fetch(){
jQuery.ajax({
url:“”,
键入:“post”,
数据:{action:'data_fetch',exactwords:jQuery('#usp title').val()},
成功:功能(数据){
var text2;
var text2B;
text2=jQuery('#usp title').val();
text2B=text2.toLowerCase();
jQuery(“#datafetch”).html(data.promise().done(function()){
jQuery(“#datafetch ul li h2 a”)。每个(函数(){
var$this=jQuery(this);
if($this.text().toLowerCase()!==text2B){
$this.parent().parent().hide();
}else if(text1B==text2B){
jQuery(“#componi”).attr(“disabled”、“disabled”).hide();
}
});
});
}
});
}

实际上我们以前在这里见过,名字不错;-)干杯,很高兴它成功了!是的,我们有一个很好的名字:-实际上我们以前在这里见过,很好的名字;-)干杯,很高兴它成功了!是的,我们有一个很好的名字