私のWebアプリケーションでは、IDが#billing_address_1
のフィールドがあります。ユーザーはこのフィールドに住所を入力できます。また、オートコンプリートから値を選択することもできます。ブラウザのオートコンプリートでは作成されていないことに注意してください。別のスクリプトで値の変更を聞きます
他のjsファイルには、#billing_address_1
の変更をリッスンするイベントリスナーがあります。それは古い値を印刷しますオートコンプリートから選択した値の代わりに、印刷のため、自分のアドレスでたときにユーザタイプスクリプトが良い作業
jQuery(function($){
$('#billing_address_1').on('change paste keyup blur', function(){
console.log($(this).val());
});
});
、しかし、ユーザがオートコンプリートから値を選択した場合:スクリプトです。例。ユーザータイプはband
で、次に自動完成からbandung
を選択すると、band
がjsコンソールに印刷されます。
オートコンプリートコード:
jQuery(function($){
$('<div id="addr-find">').appendTo('body').css({
'position': 'absolute',
'display':'none',
'top':'0',
'left':'0',
'padding':'10px',
'background': '#fff',
'box-shadow':'0 0 2px #ccc',
'max-height': '250px',
'overflow-y':'auto'
});
$(document).on('click', function(e){
if($(e.target).parents('#addr-find').length == 0 && e.target.id != 'billing_address_1'){
$('#addr-find').hide();
}
});
$('#billing_address_1').on('focus', function(){
$('#addr-find').show();
});
$('#billing_address_1').on('keyup', function(){
var data = {
action: 'search_address',
addr: $(this).val()
}, $this = $(this), borderWidth = ($this.css('borderWidth').length) > 0 ? parseInt($this.css('borderWidth').replace(/[^\d]/g, '')):0;
var top = $this.offset().top + $this.innerHeight() + borderWidth;
var left = $this.offset().left;
var width = $this.innerWidth() + borderWidth
console.log(top, $this.innerHeight(), borderWidth);
$('#addr-find').css({
"top": top,
"left": left,
"width": width
}).html('Loading...').show();
$.post(AddrFind.ajaxurl, data, function(response){
if(response.length > 0){
lis = [];
for(var i = 0; i < response.length; i++){
lis.push('<li style="border-bottom:1px solid #ddd;padding-bottom:5px;margin-bottom:5px"><a href="#" class="list-addr">'+response[i]+'</a></li>');
}
$('#addr-find').html('<ul style="margin:0;list-style:none">'+lis.join('')+'</ul>').show();
$('body').off('click').on('click', '#addr-find a', function(e){
e.preventDefault();
$this.val($(this).text());
$('#addr-find').hide();
});
}
}, 'json');
});
});
でこの男さんの投稿へ
おかげで、問題を引き起こしている実際のコードを表示します。オートコンプリートのコードが問題の場合は、それを表示して、あなたが与えたものを手助けすることはできません。 –
ポイントは、どのようにajaxやjavascriptで動的に変化する入力フィールドを聞くかです。 –