如何在jQuery中更改自动完成的值输出的值?

发布于 2025-01-24 17:08:10 字数 479 浏览 3 评论 0原文

我开始学习jQuery。但是我找不到解决这个“简单”问题的解决方案吗? 当我在自动完成输入框中选择“标签”时,它最终会显示“值”。 但是我希望“标签”留在该输入框中,并希望将“值”放入隐藏的输入框中。这是我目前的自动完成的代码:

$("#input_autocomplete").autocomplete({
minLength: 2,
source: availableTags,
change: function(e, ui) {
    if (!ui.item) {
        $(this).val("");
    }
},
response: function(e, ui) {
    if (ui.content.length == 0) {
        $(this).val("");
    }
}
}).on("keydown", function(e) {
if (e.keyCode == 27) {
    $(this).val("");
}
});

I'm starting to learn jQuery. But I can't find the solution to this "simple" problem?
When I choose a "label" in my autocomplete inputbox, it eventually displays the "value".
But I want the "label" to stay in that input box and the "value" I want to put in a hidden inputbox. This is my code for autocomplete for the moment:

$("#input_autocomplete").autocomplete({
minLength: 2,
source: availableTags,
change: function(e, ui) {
    if (!ui.item) {
        $(this).val("");
    }
},
response: function(e, ui) {
    if (ui.content.length == 0) {
        $(this).val("");
    }
}
}).on("keydown", function(e) {
if (e.keyCode == 27) {
    $(this).val("");
}
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2025-01-31 17:08:10

要执行您需要的事情,您可以保留label/value用于填充jqueryui autococtlete的对象,但是map()它仅标签内容的平面数组。这避免了选择项目时要设置的值。

然后,当选择项目时,您可以使用filter()在原始数组中查找该值以找到其值。然后,您可以用它手动更新表单控件。

尝试此示例:

let availableTags = [{ label: "Foo", value: 1 }, { label: "FooBar", value: 2 }, { label: "Bar", value: 3 }, { label: "BarFoo", value: 4 }]
let $hiddenValue = $('#hidden-value');

$("#input_autocomplete").autocomplete({
  minLength: 2,
  source: availableTags.map(t => t.label),
  select: function(e, ui) {
    let id = availableTags.filter(tag => tag.label === ui.item.label)[0].value;
    $hiddenValue.val(id);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<input type="text" id="input_autocomplete">
<input type="text" id="hidden-value" readonly />

To do what you require you can retain the array of label/value objects used to populate a jQueryUI Autocomplete, but map() it to a flat array of just the label contents. This avoids the value being set when an items is selected.

Then when an item is selected you can use filter() to lookup that value in the original array to find its value. Then you can manually update the form control with that.

Try this example:

let availableTags = [{ label: "Foo", value: 1 }, { label: "FooBar", value: 2 }, { label: "Bar", value: 3 }, { label: "BarFoo", value: 4 }]
let $hiddenValue = $('#hidden-value');

$("#input_autocomplete").autocomplete({
  minLength: 2,
  source: availableTags.map(t => t.label),
  select: function(e, ui) {
    let id = availableTags.filter(tag => tag.label === ui.item.label)[0].value;
    $hiddenValue.val(id);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<input type="text" id="input_autocomplete">
<input type="text" id="hidden-value" readonly />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文