2011-05-23 22 views
1

好吧,所以我得到了元素編碼等。它有點奇怪,因爲如果表單是空的,一個清晰的按鈕會以圖像形式顯示:(x)顯示一個元素,如果文本輸入不是空的使用jQuery

這是腳本,頁面加載時jQuery重點關注文本輸入字段,同時顯示(x)清除類。我希望達到的是這個。

在頁面加載時,jQuery仍然關注文本輸入字段,但它不顯示清晰的類(x),但只有在輸入文本字段中至少有1個字符時纔會顯示它,其他vise不顯示。

這裏是當前的代碼

$(document).ready(function() { 
      $('#ui_query').focus(); 
     }); 

     $('#ui_query').focus(function() { 
      $('.clear-helper').css('opacity','0.9999'); 
     }); 

     $(document).focusout(function() { 
      $('.clear-helper').css('opacity','0.3333'); 
     }); 



     (function ($, undefined) { 
     $.fn.clearable = function() { 
     var $this = this; 
     $this.wrap('<div class="clear-holder" />'); 
     var helper = $('<span class="clear-helper"></span>'); 
     $this.parent().append(helper); 
     helper.click(function(){ 
      $this.val(""); 
      $('#ui_query').focus(); 
     }); 
     };  
     })(jQuery); 

     $("#ui_query").clearable(); 

回答

0

可以使用attribute-not-equals屬性選擇與非空值的目標輸入:

$('#ui_query[value!=""]').doSomething(); 

如果你不想要一個空格字符來算作爲「某事」,那麼你可以這樣做:

if($.trim($('#ui_query').val()) !== "") { 
    $('.clear-helper').css('opacity','0.9999'); 
} 
相關問題