[英]Autocomplete : prevent Change event after Select event
A user can enter an input value : 用户可以输入一个输入值:
The problem is that the "geo" function is called twice, because the change event is called after the select event. 问题是“ geo”函数被调用了两次,因为change事件在select事件之后被调用。
Here is my code : 这是我的代码:
<input id="test">
$("#test").autocomplete({
minLength: 2,
source: "file.php",
select:function(event,ui){
var name = ui.item.value;
geo(name);
},
change: function (event, ui) {
$.ajax({
type:'POST',
async:false,
url:'file2.php',
data:{'name':$('#test').val()},
success:function(r){
o=JSON.parse(r);
geo(o.name);
}
});
}
});
I tried to add event.preventDefault(); 我试图添加event.preventDefault(); like this :
像这样 :
select:function(event,ui){
var name = ui.item.value;
geo(name);
event.preventDefault();
},
but it doesn't work... 但这行不通...
Any idea? 任何想法?
Example to try: 尝试的示例:
var selectedName = '';
$("#test").autocomplete({
minLength: 2,
source: "file.php",
select:function(event,ui){
selectedName = ui.item.value;
geo(selectedName);
},
change: function (event, ui) {
if (selectedName === $('#test').val()) {
return;
}
$.ajax({
type:'POST',
async:false,
url:'file2.php',
data:{'name':$('#test').val()},
success:function(r){
o=JSON.parse(r);
geo(o.name);
}
});
}
});
I think the change & select callback is different with input element events change & select. 我认为change&select回调与输入元素事件change&select不同。
selecting a list item trigger select, then change typing just trigger change 选择一个列表项触发选择,然后更改键入只是触发更改
typing trigger "input/keyup/keydown/keypress" event, and when blur trigger "change" event 键入触发“输入/键盘输入/按下/按键”事件,以及模糊触发“更改”事件
How about remove select callback here. 如何在此处删除选择回调。 Regardless of typing or selecting, change triggered anyway.
无论键入或选择,更改都会触发。 If you don't wanna a checking on selecting, add a determine statements before ajax.
如果您不想检查选择内容,请在ajax之前添加确定语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.