2017-04-09 57 views
0

我有一个input和一些结果,如果有任何结果与其文本标题与输入的确切.val()不匹配,则应删除其.parent().parent()。我有被靶向错this为我做$this.parent().parent().hide();$this问题是var $this = jQuery(this).text().toLowerCase();如何删除元素,如果其文本不符合其他元素文本?

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).text().toLowerCase(); 
      if ($this != text2B) { 
      $this.parent().parent().hide(); 
      } else if (text1B == text2B) { 
      jQuery("#componi").attr("disabled", "disabled").hide(); 
      } 
     }); 
     }); 
    } 
    }); 
} 

回答

3

的问题是,要设置$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(); 
      } 
     }); 
     }); 
    } 
    }); 
} 
+0

优秀,很好地工作。非常感谢 –

+0

其实我们以前见过这里,不错的名字;-) –

+1

干杯,很高兴它的工作!是的,我们有 - 确实很好的名字:-P –

0

只是做$(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(); 
    } 
} 
相关问题