【问题标题】:Boostrap typehead (auto complete) ajax return multiple valuesBootstrap typeahead(自动完成)ajax返回多个值
【发布时间】:2018-06-23 04:47:34
【问题描述】:

我使用 boostrap typhead 创建自动完成

我想在从自动完成中选择数据时显示, 返回2个值并放置到输入框和隐藏数据

我发现了 2 个问题

  1. 带双引号的数据自动完成
  2. 我无法将 ID BIO 置于隐藏类型中

这是我的代码

PHP

<?php
include "database.php";
$query = $_REQUEST['query'];
$result = mysqli_fetch_object(mysqli_query($con,"select count(*) as total from biodata where NameBiodata LIKE '%".$query."%' "));
    if ($result->total > 0) {
        $data_tsk = mysqli_query($con,"select  IdBio,NameBiodatafrom biodata where NoUrut  NameBiodata LIKE '%".$query."%'");
        $data = array();
        while($tsk=mysqli_fetch_object($data_tsk)){ 
            array_push($data,array('label'=>$tsk->NameBiodata,'value'=>$tsk->IdBio));
        }
        echo json_encode($data);
    }
?>

Javascript

$('#name_biodata').typeahead({
          source: function (query, process) {
            $.ajax({
                url: 'ajax/auto_complete_biodata.php',
                type: 'POST',
                dataType: 'JSON',
                data: 'query=' + $('#name_biodata').val(),
                success: function(data) {
                    var results = data.map(function(item) {
                        var someItem = { myname: item.label, myvalue: item.value};
                        var jsonString = JSON.stringify(someItem.myname);  
                        return jsonString; // I don't know how to remove double qoutes
                        //return jsonString.replace(/\"/g, "");     // I try to remove double qoutes, but not selected
                    });
                    return process(results);
                }
            });
        },
        minLength: 1,
        updater: function(item) {
            var obj = JSON.parse(item);
            console.log(obj);  // i can't get Id BIO :(
            $("#IdBio").val(obj.value);
            return item;
        }
      }); 

JSON

[{"label":"Yeni Adelia Sofiyah als Selvi","value":"151"},{"label":"Yenni Ginting als Kak Yen","value":"276"},{"label":"Ria Wira","value":"572"}]

帮帮我谢谢

【问题讨论】:

    标签: javascript php jquery ajax twitter-bootstrap


    【解决方案1】:
    1. 双引号是由JSON.stringify 引起的。当您想通过 HTTP 请求发送 JSON、将 JSON 作为字符串回显等时,此方法很有用。在您的情况下,只需使用 var jsonString = someItem.myname;

    2. 您的代码在这里通常是错误的。 source 应该是这样的

    source: [ {name: "name", any: "value", you: "want"}, {name: "name2", any: "value", you: "want"} ];

    name 字段是必需的。如果您想传递对象(具有名称和其他值)而不是单个值,那么如果没有它,则预先输入将不起作用。在你的情况下,你得到

    source: ['one', 'two', 'three', 'etc'];
    

    所以你应该这样做

      source: function (query, process) {
        $.ajax({
            url: 'ajax/auto_complete_biodata.php',
            type: 'POST',
            dataType: 'JSON',
            data: 'query=' + $('#name_biodata').val(),
            success: function(data) {
                var results = data.map(function(item) {
                    var someItem = { name: item.label, myvalue: item.value}; 
                    return someItem;
                });
                return process(results);
            }
        });
    },
    

    现在你会得到

    [
      {name: 'name_from_label', myvalue: 'your_value'}
    ]
    

    就是这样。

    【讨论】:

    • 我已经受够了。但不能选择。在错误控制台中显示 SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
    • @MuhamadRidwansyah 那是因为您试图在 updater 方法中将 JSON 对象解析为 JSON。您不需要创建obj var,只需使用item
    • 它的工作,当已经评论时 // minLength: 1, // updater: function(item) { // var obj = JSON.parse(item); // console.log(obj); // 归还物品; // } 那么如何放置 >> $("#IdBio").val(obj.value);
    • 好的工作。我像这样修复。 minLength: 1, 更新器: function(item) { $('#IdBio').val(item.myvalue);归还物品; } 谢谢沃伦
    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2016-05-07
    • 2022-06-24
    • 2015-01-16
    • 2018-05-14
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多