【问题标题】:jQuery Autocomplete - Override label / value in source datajQuery Autocomplete - 覆盖源数据中的标签/值
【发布时间】:2011-01-03 15:01:50
【问题描述】:

自动完成小部件似乎要求数据在数组中具有变量“标签”和“值”。

是否可以覆盖它并使用您自己的值(例如数据库中的列),以便您可以在表单中拥有多个具有自动完成功能的文本框?

给使用 PHP 和 MySQL 自动完成功能的人的一些提示...

while($r = mysql_fetch_assoc($rsCustomerLookup)) {
$rows[] = $r;
}
print json_encode($rows);

从数据库中获取 json 格式的数据

select: function( event, ui ) { 
    $( "#name" ).val(ui.item.name);
    $( "#surname" ).val(ui.item.surname);
    $( "#company" ).val(ui.item.company);
    $( "#address1" ).val(ui.item.address1);
    $( "#address2" ).val(ui.item.address2);
     etc

如果您打算从一个自动完成文本框填充整个表单,则用数据填充其他文本框

【问题讨论】:

    标签: php mysql jquery-ui


    【解决方案1】:

    您可以将value 设置为记录的数据库ID。然后,您可以获取 value(这是数据库 ID)并获取 namesurnamecompany 等。

    【讨论】:

    • 不确定您的意思?我可以在创建数组时重写 DB 列名,这样无论数据库 ID 列被命名为“标签”,我想要的值列都被重命名为“值”。只是希望我可以告诉 javascript 在 json 数据中使用什么值,而无需编辑 jquery 小部件的源(这是我目前正在做的)
    【解决方案2】:

    enter link description here

    <script> 
    $().ready(function() {
    $('#tag').autocomplete('tag.php?find=tag', {
              width: 260,
              matchContains: true,
              selectFirst: false
            });
    });
    
        $('#other').autocomplete('tag.php?find=other', {
              width: 260,
              matchContains: true,
              selectFirst: false
            });
    });
    </script>
    
    <?php
    //in tag.php
    $find = $_GET["find"];
    if($find=='tag'){
        $q = strtolower($_GET["q"]);
        if (!$q) return;
        $sql = "select DISTINCT tag from tag where name_tag LIKE '%$q%'";
        $rsd = mysql_query($sql);
        while($rs = mysql_fetch_array($rsd)) {
            $cname = $rs['name_tag'];
            echo "$cname\n";
        }
    }
    /*
    This adds to other data
        if($find=='other'){
        $q = strtolower($_GET["q"]);
        if (!$q) return;
        $sql = "select DISTINCT tag from tag where name_tag LIKE '%$q%'";
        $rsd = mysql_query($sql);
        while($rs = mysql_fetch_array($rsd)) {
            $cname = $rs['name_tag'];
            echo "$cname\n";
        }
    }
    */
    ?>
    
    <input id='tag' type='text'>
    <input id='other' type='text'>
    

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      相关资源
      最近更新 更多