2017-04-09 71 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(); 
    } 
} 
相關問題