2017-08-15 68 views
-1

我在輸入上綁定了模糊。在模糊處,我想使用$(this)並檢測此輸入。有沒有辦法做到這一點?模糊元素

<input data-john="x" data-doe="false"> 

$('[data-john=x]:not([data-doe=true])').blur(function() { 
    console.log($(this)) 
}) 

在此示例中,$(this)正在返回Window,而不是模糊元素。

有沒有辦法用$(this)得到輸入?

+0

它不返回窗口,除非你改寫'$'函數來進行。如果它仍然是jQuery的''',那麼'$(anything)'返回一個jQuery對象實例。 – Kaiido

回答

1

您可以使用$('input')blur()事件和有this$(this)console.log

$('input').blur(function() { 
 
    console.log(this); 
 
    console.log($(this).data('john')); 
 
    console.log($(this).data('doe')); 
 
    console.log($(this)); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input data-john="x" data-doe="false">

+0

這是一個錯字。我更新了這個問題。你能檢查嗎? – senty

+0

您仍然可以使用相同的代碼。我只是做了小小的更新,將您的HTML添加到我的代碼片段中。請檢查。 –