Jquery Autocomplete display names and send Id

时间:2017-12-18 08:35:16

标签: javascript jquery jquery-ui-autocomplete

How can I change my jQuery autocomplete to use an Id instead of my values? I would like to display the list of names but send a search using the Id value. Now it is working properly with the names but I think it will be more effective if it tries to find a unique value as an ID.

mask = (np.max(image, axis=-1) > threshold).astype(int)

2 个答案:

答案 0 :(得分:1)

您可以添加隐藏字段并使用on select事件将隐藏字段的值设置为所选ID

http://api.jqueryui.com/autocomplete/#event-select

您还可以使用格式为[{value: 'value', label: 'label'}]的选择数据,但使用这种方式,该字段将显示ID而不是标签

var availableTags = [
      {id: 1, label: "ActionScript"},
      {id: 2, label: "Ruby"},
      {id: 3, label: "Scala"},
      {id: 4, label: "Scheme"}
    ];
    availableTags2 = [
      {value: 1, label: "ActionScript"},
      {value: 2, label: "Ruby"},
      {value: 3, label: "Scala"},
      {value: 4, label: "Scheme"}
    ];
    $( "#a" ).autocomplete({
      source: availableTags,
      select: function( event, ui ) {
        $('#tosend').val(ui.item.id);
      }
    });
    $( "#b" ).autocomplete({
      source: availableTags2
    });
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script
			  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
			  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
			  crossorigin="anonymous"></script>
id to send<br>
<input type="text" name="tosend" id="tosend"><br><br>
type below<br>
<input id="a"><br>
<br>
<br>
using value instead of id<br>
<input id="b">

答案 1 :(得分:0)

我对你的后端一无所知。但假设它正在接受您的搜索参数的ID,是否会将source值更改为sellersID以解决您的问题?在源之后你还有额外的逗号。这会给你带来麻烦。

$("#searchSeller").autocomplete({
  messages: {
    noResults: 'No sellers with this ID.',        
  },
  minLength: 2,
  delay: 500,
  source: sellersID
});