jquery - 选择文本输入上的标签,但在不更改文本值的情况下发布值
问题描述
我希望能够使用自动完成功能来搜索标签列表,然后发布值。这已成功完成。问题是选择标签时,它会更改为值。因此,用户选择了一个自动完成的值,比如 burger,但在选择时,它会更改为 id,比如 18。在帖子中,它实际上发送了正确的信息,即 id。当人们在选择时看到一个数字而不是一个值时,这可能会让他们感到困惑。第一次使用jquery,如果它很简单,请原谅我。
数据库提取 get_meals.php:
while($row = $stmt->fetch()) {
$return_arr[] = array('value'=>$row['food_id'], 'label' => $row['food']);
}
这是jQuery:
<script type="text/javascript">
$(function() {
$(".auto").autocomplete({
source: "get_meals.php",
minLength: 0
});
});
</script>
尝试过类似的东西:
select: function (event, ui) {
event.preventDefault();
$(this).val(ui.item.label);
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false; // Prevent the widget from inserting the value.
select: function(event, ui) {
$('#search').val(ui.item.label);
$('#searchval').val(ui.item.value);
return false; // Prevent the widget from inserting the value.
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false; // Prevent the widget from inserting the value.
解决方案
我不得不添加一个隐藏字段:
<input type="hidden" id="food_id" name="food_id" value="" class='auto'>
<input type='text' id="food" name='food' value="" class='auto'>
然后自动完成以下内容:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$(".auto").autocomplete({
source: "add_meals_frm_.php",
minLength: 0,
select: function(event, ui) {
$('#food_id').val(ui.item.value);
event.preventDefault();
$("#food").val(ui.item.label);
}
});
});
</script>
推荐阅读
- javascript - 我想在画布渲染后收到警报
- ibm-watson - 如何通过 Watson Workspace API 将应用程序添加到工作区?
- javascript - JSON 提取数据返回次数过多
- vb.net - 需要帮助将字符串与带有循环的列表框进行比较
- sql - 将字符串转换为日期时间适用于选择但也不更新(T-SQL)
- scala - 如何在 Spark 中的 DataFrame groupBy 聚合中执行算术运算?
- javascript - Javascript 更新对象属性值
- javascript - IE11 - 变量超出范围,即使我使用 `let` 进行循环
- java - 在 Java 中使用 Google Guava 散列输出小写字符,而在线散列站点输出大写
- c# - 使用 HttpClient 的简单 POST