2017-04-24 9 views
-1

私の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'); 
    }); 
}); 
+0

でこの男さんの投稿へ

if (!Object.prototype.watch) { Object.defineProperty(Object.prototype, "watch", { enumerable: false , configurable: true , writable: false , value: function (prop, handler) { var oldval = this[prop] , newval = oldval , getter = function() { return newval; } , setter = function (val) { oldval = newval; return newval = handler.call(this, prop, oldval, val); } ; if (delete this[prop]) { // can't watch constants Object.defineProperty(this, prop, { get: getter , set: setter , enumerable: true , configurable: true }); } } }); } // object.unwatch if (!Object.prototype.unwatch) { Object.defineProperty(Object.prototype, "unwatch", { enumerable: false , configurable: true , writable: false , value: function (prop) { var val = this[prop]; delete this[prop]; // remove accessors this[prop] = val; } }); } 

おかげで、問題を引き起こしている実際のコードを表示します。オートコンプリートのコードが問題の場合は、それを表示して、あなたが与えたものを手助けすることはできません。 –

+0

ポイントは、どのようにajaxやjavascriptで動的に変化する入力フィールドを聞くかです。 –

答えて

1

ここで実際に何が起こるには、オートコンプリート、フィールドに値を代入する前に、入力フィールドの値を印刷しているです。ここでオートコンプリートコードを入力できる場合は、回答を提案することができます。 私はこれがコメントだと知っていますが、私はオプションがありませんが、評判のマイルストーンが50に達した時点でコメントを得るため、ここでコメントするだけです。

編集:

これは古い投稿のコピーコードです。これはobj protoype watchイベントを作成するはずです。 時計ではなく、変更イベントを使用すると、あなたの問題を解決する必要がありますGitHubの https://gist.github.com/eligrey/384583

+0

ok..please check updated質問を更新しました。 –

+0

ヒントを使用して "オートコンプリートの前に値を割り当てます"。私は 'console.log' stufにsetTimeoutを追加しました。素晴らしい仕事。 –

+0

このような場合にタイムアウトを使うのは悪い習慣です。私は笑います。この時計を使ってみてください –

関連する問題