jQuery UI AutoComplete コントロールを使用しています(jQuery UI 1.8.1 に更新されたばかりです)。ユーザーがテキスト ボックスを離れるたびに、テキスト ボックスの内容を既知の適切な値に設定し、選択された値に非表示の ID フィールドを設定したいと考えています。さらに、テキストボックスの内容が変更されたときにページをポストバックしたい.
現在、オートコンプリートの選択イベントに非表示の ID を設定してから、テキスト ボックスの値を設定し、必要に応じてポスト バックを発生させるテキスト ボックスの変更イベントを設定することで、これを実装しています。
ユーザーがキーボードのみを使用する場合、これは完全に機能します。入力し、上矢印と下矢印を使用して値を選択し、タブで終了します。select イベントが発生し、id が設定されてから、change イベントが発生し、ページがポストバックされます。
ただし、ユーザーが入力を開始してからマウスを使用してオートコンプリート オプションから選択すると、change イベントが発生し (フォーカスがオートコンプリート メニューに移動すると)、select イベントが ID を設定する前にページがポストバックされます。
マウスが使用されている場合でも、選択イベントの後まで変更イベントが発生しないようにする方法はありますか?
$(function() {
var txtAutoComp_cache = {};
var txtAutoComp_current = { label: $('#txtAutoComp').val(), id: $('#hiddenAutoComp_ID').val() };
$('#txtAutoComp').change(function() {
if (this.value == '') { txtAutoComp_current = null; }
if (txtAutoComp_current) {
this.value = txtAutoComp_current.label ? txtAutoComp_current.label : txtAutoComp_current;
$('#hiddenAutoComp_ID').val(txtAutoComp_current.id ? txtAutoComp_current.id : txtAutoComp_current);
} else {
this.value = '';
$('#hiddenAutoComp_ID').val('');
}
// Postback goes here
});
$('#txtAutoComp').autocomplete({
source: function(request, response) {
var jsonReq = '{ "prefixText": "' + request.term.replace('\\', '\\\\').replace('"', '\\"') + '", "count": 0 }';
if (txtAutoComp_cache.req == jsonReq && txtAutoComp_cache.content) {
response(txtAutoComp_cache.content);
return;
}
$.ajax({
url: 'ajaxLookup.asmx/CatLookup',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: jsonReq,
type: 'POST',
success: function(data) {
txtAutoComp_cache.req = jsonReq;
txtAutoComp_cache.content = data.d;
response(data.d);
if (data.d && data.d[0]) { txtAutoComp_current = data.d[0]; }
}
});
},
select: function(event, ui) {
if (ui.item) { txtAutoComp_current = ui.item; }
$('#hiddenAutoComp_ID').val(ui.item ? ui.item.id : '');
}
});
});