2017-01-25 129 views
1

我遇到了問題。使用jQuery時,輸入有一些值時,更改事件無法觸發。我在下面解釋我的代碼。使用jquery自動完成時無法觸發更改事件

<input name="" type="text" class="inputfield" id="auto"> 
var city = [ 
    'Alabama', 'Alaska', 'Arizona' 
]; 

$('#auto').autocomplete({ 
    source:[city], 
    minLength: 1, 
    change: function (event, ui) { alert('hello'); } 
}); 

在這裏,我需要當一個值將選擇城市陣列輸入字段更改事件應觸發未在我的情況發生。請幫幫我。

+0

嘗試使用'select'而不是'change'... select:function(event,ui){alert('hello'); } – Roy

回答

0

添加自動對焦和重點消防警報

var city = [ 
 
    'Alabama', 'Alaska', 'Arizona' 
 
]; 
 

 
$('#auto').autocomplete({ 
 
    source: city, 
 
    minLength: 1, 
 
    autoFocus: true, 
 
focus: function(event, ui) { 
 
     alert('hello'); 
 
    } 
 
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
    <link rel="stylesheet" href="/resources/demos/style.css"> 
 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
<input name="" type="text" class="inputfield" id="auto">

2

change事件工作正常,但請注意,只有在對其值進行更改後元素模糊時纔會觸發。

如果您希望事件,火災時作出選擇,使用select事件:

$('#auto').autocomplete({ 
    source: city, 
    minLength: 1, 
    select: function(event, ui) { 
     alert('hello'); 
    } 
}); 

Working example

+0

太棒了!快速而快速的迴應。 –

+0

仍然沒有在我的代碼工作。你可以提到jquery自動完成版本。 – subhra

+0

它使用jQueryUI 1.92。你可以在小提琴中檢查它。另外,請檢查您的代碼是否有錯誤 –

相關問題