簡體   English   中英

在 Jquery 中獲取自動完成的選定值

[英]Getting the selected value on autocomplete in Jquery

我在 HTML 模板上的表單中添加了自動完成功能,我想在選擇提示時執行一些操作,有什么辦法嗎? 我正在使用Jquery-Typeahead 這是我的實際代碼:

$(document).ready(function(){

  // Defining the local dataset
  $.getJSON('http://127.0.0.1:8000/myapi', function(data) {
    console.log(data)

    var dt = data

    $(() => {

      $('#myform').typeahead({
        source: {
          data: dt.results.map(record => record.item)
        },
        callback: {
          onInit: function($el) {
            console.log(`Typeahead initiated on: ${$el.prop('tagName')}#${$el.attr('id')}`);
          },
          onClick: function() {
            console.log(); //How can i console.log() the selected value here, for example?
          }
        }
      });

    });

  });
});

嘗試定義一個onClickAfter回調,它會在用戶單擊某個項目后onClickAfter調用。 像這樣的東西:

 onClickAfter: function(node, a, item, event) { // item will be the item you selected console.log(item); }

你也可以用同樣的方式定義onClickBefore回調,它會在“正常”輸入行為開始之前立即被調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM