2010-05-12 196 views
4

嗨我遇到了更改事件的问题。 By documntation should be object ui.itemJquery UI自动完成事件更改

选择一个项目后; ui.item引用所选项目。总是在关闭事件后触发。

但是当我尝试它ui.item是未定义:(我想取消设置s_town_id时自动完成输入不匹配从脚本数据。

<input id="s_town" type="text" name="s_town" /> 
<input type="text" id="s_town_id" name="s_town_id" /> 
 

    
$(function() { 
    $("#s_town").autocomplete({ 
    source: function(request, response) { 
    $.ajax({ 
    url: "/_system/_ajax/uiautocomplete.php", 
    dataType: "json", 
    data: { 
     name: "s_town", 
     term: request.term 
    }, 
    success: function(data) { 
     response($.map(data, function(item) { 
     return { 
     label: item.whisper_name+ " [" + item.zip_code + "/" + item.lup_state + "]", 
     value: item.whisper_name, 
     id: item.whisper_id, 
     zip_code: item.zip_code, 
     lup_state: item.lup_state, 
     stateid: item.stateid 
     } 
     })) 
    } 
    }) 
    }, 
    minLength: 2, 
    select: function(event, ui) { 
    $("#s_town_id").val(ui.item.id); 
    }, 
    change: function(event, ui) 
    { 
    // ui.item is undefined :(where is the problem? 
    $("#s_town_id").val(ui.item.id); 
    } 

    }); 


}); 
    

 
+0

嗨Stenly,我有完全相同的问题,并不得不使用相同的解决方法。你能找到更好的解决方案吗? – blacktie24 2011-04-02 22:49:11

+1

hmm意识到问题在于我从教程中加载了源代码,该教程的版本为1.8,对于任何最终出现此问题的人,这个问题在1.8.11中得到修复。 http://bugs.jqueryui.com/ticket/5490 – blacktie24 2011-04-03 04:39:05

回答

6

我找出解决方案,我测试event.originalEvent.type如果meneuselected与否和失败我没有设置s_town_id后但没有更好的解决方案仍然是惠康。

<input id="s_town" type="text" name="s_town" /> 
<input type="text" id="s_town_id" name="s_town_id" /> 
 

    
$(function() { 
    $("#s_town").autocomplete({ 
    source: function(request, response) { 
    $.ajax({ 
    url: "/_system/_ajax/uiautocomplete.php", 
    dataType: "json", 
    data: { 
     name: "s_town", 
     term: request.term 
    }, 
    success: function(data) { 
     response($.map(data, function(item) { 
     return { 
     label: item.whisper_name+ " [" + item.zip_code + "/" + item.lup_state + "]", 
     value: item.whisper_name, 
     id: item.whisper_id, 
     zip_code: item.zip_code, 
     lup_state: item.lup_state, 
     stateid: item.stateid 
     } 
     })) 
    } 
    }) 
    }, 
    minLength: 2, 
    select: function(event, ui) { 
    $("#s_town_id").val(ui.item.id); 
    }, 
    change: function(event, ui) 
    { 
    try 
    { 
     if(event.originalEvent.type != "menuselected") 
     { 
      // Unset ID 
      $("#s_town_id").val(""); 
     } 
    } 
    catch(err){ 
     // unset ID 
     $("#s_town_id").val(""); 
    } 
    } 

    }); 


}); 
    

 
0

如果未定义ui.item,那意味着您的json源代码格式不正确。 你必须发送一个JSON来源是这样的:

[{"label":"Jean","value":1},{"label":"carl","value":2}] 

您可以添加更多的关键阵列,但至少你必须设置“标签”和“值”。 检查json字符串。 此外,我认为你现在使用最新版本的自动完成1.8.1

+0

是的,但我想要的解决方案,当输入值不匹配DB中的任何记录。所以在那一刻JSON是空的。我想要的功能可以被描述为必须匹配。 – 2010-06-02 10:42:02