我如何获取我在Jquery自动完成中选择的选项的值(ID)Acc

时间:2019-03-15 09:43:34

标签: jquery laravel

我正在尝试使用Jquery自动完成功能在我的网站中构建一个部分

<li> 
  <input id="tags" id="srch_term" name="srch_term" class="form-control">
</li>
$(document).ready(function() {
  var location = [];
  $.ajax({
    url: $('#hf_base_url').val() + '/ajax/all_location_processing',
    type: 'GET',
    cache: false,
    dataType: 'json',    
    headers: { 
      'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content') 
    }, 
    success: function(data) {
      $.each(data, function(key, val) { 
        location.push({
          id: val.id,
          name:val.name
        }); 
      })
    },
    error:function() {}
  });
});

这是我在页面加载时获取所有位置并将location_idlocation_name存储在名为location的数组中所做的工作。

{ id: 24, name: "Baltimore" },
{ id: 25, name: "Brooklyn" },
{ id: 26, name: "Chicago" },
{ id: 27, name: "Cleveland" },
{ id: 28, name: "Denver" },
{ id: 29, name: "Detroit" },
{ id: 30, name: "Edison" },
{ id: 31, name: "Five Towns" },
{ id: 32, name: "Flatbush" },
{ id: 33, name: "Israel"},
{ id: 34, name: "Lakewood" },
{ id: 35, name: "Los Angeles" },
{ id: 36, name: "Miami" },
{ id: 37, name: "Monsey" },
{ id: 38, name: "Montreal" },
{ id: 39, name: "Passaic" },
{ id: 40, name: "Philadelphia" },
{ id: 41, name: "Tornoto" },

现在在自动完成中选择位置时我要做什么,然后我想获取location_id,以便我可以做进一步的任务

1 个答案:

答案 0 :(得分:1)

<input type="hidden" id="id_location_hidden" val="">

检查此代码,希望对您有所帮助。

$("#srch_term").autocomplete({
    source: url,
    select: function( event, ui ) {
        event.preventDefault();
        $("#id_location_hidden").val(ui.item.id);
    }
});