【问题标题】:get the selected value,id from autocomplete [closed]从自动完成中获取选定的值,id [关闭]
【发布时间】:2014-12-22 10:26:44
【问题描述】:

我想从自动完成中获取选定的值,id

这是我的代码

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.core.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.widget.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.position.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.autocomplete.min.js"></script>
<link rel="stylesheet" href="http://www.jqueryautocomplete.com/js/jquery/smoothness/jquery-ui-1.8.16.custom.css"/>
<script>
$(function() {

    $( "#search" ).autocomplete(
    {
         source:'source.php',
    })
});
</script>
<input type="text" id="search" class="ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">

查询是

$req = "SELECT id,name "."FROM mytable "."WHERE name LIKE '%".$_REQUEST['term']."%' "; 

【问题讨论】:

  • 您想更改不同操作(如focusselect)的行为,还是更改项目的呈现方式?
  • @super-qua 感谢您的回复..我只想在选择项目时获取 id。如何使用 select 获取 id。

标签: php jquery autocomplete


【解决方案1】:

一般autocomplete 正在寻找具有labelvalue 集合的项目。 对于您的情况,您必须进行调整,因此自动完成功能使用您的 idname 字段。

您可以覆盖select event 并使用ui.item.idui.item.name 访问字段。这看起来像这样:

$( "#search" ).autocomplete(
{
     source:'source.php',
     select: function(event, ui) {
        // handle the user selecting the entry
        // access fields with ui.item.name and ui.item.id
     }

})

但是,这可能会导致您的数据无法正确显示,因为自动完成默认使用 labelvalue 字段。

要覆盖显示行为,请实现_renderItem 方法来显示您的数据。 可以在源代码中的examples page 上找到如何执行此操作的示例。

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多