devbridge / jQuery-Autocomplete如何将数据部分转换为提交值

时间:2018-07-31 16:46:07

标签: jquery autocomplete

我已经在表单中使用了自动填充功能,提交后我看到“值”是帖子数据的一部分。问题是我还需要帖子中的“数据”部分,而且我似乎找不到解决方法。

这就是我正在使用的东西。

<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<script>
    var countries = [
        { value: 'Palm Bay', data: '1840015094' },
        { value: 'Palm Beach Gardens', data: '1840015125' }
    ];

    $('#autocomplete').autocomplete({
lookup: countries,
onSelect: function (suggestion) {
  $("location").val(suggestion.value);
  $("city_id").val(suggestion.data);
}
    });
</script>
<input type="hidden" name="city_id">
<input type="text" placeholder="Location: City, State" data-msg-required="Please enter a city and state." maxlength="100" class="form-control form-control-lg" autocomplete="off" name="location" id="autocomplete">

1 个答案:

答案 0 :(得分:1)

您需要根据他们的docs使用onSelect事件

<script>
var countries = [
    { value: 'Palm Bay', data: '1840015094' },
    { value: 'Palm Beach Gardens', data: '1840015125' }
];

$('#autocomplete').autocomplete({
    lookup: countries,
    onSelect: function (suggestion) {
        $("#city_id").val(suggestion.data); // Here
    }
});
</script>

<input type="hidden" name="city_id" id="city_id">
<input type="text" placeholder="Location: City, State" data-msg-required="Please enter a city and state." maxlength="100" class="form-control form-control-lg" autocomplete="off" name="location" id="autocomplete">