2014-01-18 33 views
0

要隱藏點擊一個div上我用要停止document.click功能鍵上最多

$('#display').click(function(e){ 
     e.stopPropagation(); 
    }); 


    $(document).click(function(){ 

     $('#display').slideUp(); 

}); 

現在這一頁我想使函數document.click其餘工作僅如果用戶不打字文本框中帶有ID「mse」的任何文本。

 <input type="text" id="mse" name="search"> 

我想是這樣的:

 $(document).click(function(){ 
     if($("#mse").keyup()){ 
      //do nothing 
     }else{ 
     $('#display').slideUp(); 
     } 
    }); 

不過,這並不奏效。

幫我解決這個問題。

我總代碼:

$(document).ready(function(){ 
     $("#mse").keyup(function() 
     { 
      var searchbox = $(this).val(); 
      var dataString = 'searchword='+ searchbox; 
      if(searchbox=='') 
      {} 
      else 
      { 
      $.ajax({ 
        type: "POST", 
        url: "search.jsp", 
        data: dataString, 
        cache: false, 
        success: function(html) 
        { 

        $("#smartscreen").html(html).show(); 
        } 
      }); 
     }return false; 
     }); 

     $('#mse').focus(function(){ 
      $('#display').css("display", "block"); 
     }); 

     $('#display').click(function(e){ 
       e.stopPropagation(); 
     }); 



     }); 

      $(document).click(function(e){ 

       $('#display').slideUp(); 



       }); 
+0

的點擊不應該只如果文本框爲空,或者只在用戶沒有輸入當前工作的事(即點擊按鍵同時)? – techfoobar

+0

這種情況下的邏輯(至少是解釋它的方式)沒有意義。您要麼輸入要麼點擊。不是都。當你開始點擊鼠標時,你的手會在什麼位置輸入一個單詞? – Deryck

+2

「輸入任何文字」對你來說意味着什麼? (A)重點在你提到的領域; (B)一個或多個鍵實際上是關閉的,即,沒有相應的鍵盤鍵發生鍵入事件; (C)該領域不是空的; (D)在最後x毫秒內在該字段中出現擊鍵; (E)以上全部;(F)以上都不是。 – nnnnnn

回答

0

您可能需要打開文檔的單擊事件和關閉。下面是該成分:

.on() with namespacing of the event

.off()

.focus().blur()

然後,根據您的需要,做這樣的事情:當 「MSE」 元素被集中,關閉文檔的點擊事件。當「mse」元素變得模糊時,請重新打開文檔的點擊事件。 Example in this JS Fiddle

從JS小提琴

相關代碼:

$('#mse').focus(function(){ 
    $(document).off('click.slider'); 
    $('#display').css('display', 'block'); 
}); 

$('#mse').blur(function(){ 
    $(document).on('click.slider', function(e){ 
     $('#display').slideUp(); 
    }); 
}); 
+0

感謝BRO.It工作 –