Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用ajax在select2中显示多个结果数据?_Javascript_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Javascript 如何使用ajax在select2中显示多个结果数据?

Javascript 如何使用ajax在select2中显示多个结果数据?,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我试图将表mysql中的一些字段数据显示到select2选项中,但无论我做什么,它仍然只显示一个字段数据。 这就是我所期望的: 有什么建议吗?谢谢 在javascript文件中,我有以下代码 <script type="text/javascript"> //List ajax for list product $(".js-productList").select2({ ajax: { url: "halaman/hal_search2ajax/pr

我试图将表mysql中的一些字段数据显示到select2选项中,但无论我做什么,它仍然只显示一个字段数据。 这就是我所期望的: 有什么建议吗?谢谢

在javascript文件中,我有以下代码

    <script type="text/javascript">
    //List ajax for list product
  $(".js-productList").select2({
  ajax: {
    url: "halaman/hal_search2ajax/productsList.php",
    type: "post",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
      };
    },
    processResults: function(data){
        return { results: data };
      },
    cache: true
  },
  minimumInputLength: 3,
});
</script>

//列表产品的列表ajax
$(“.js productList”)。选择2({
阿贾克斯:{
url:“halaman/hal_search2ajax/productsList.php”,
类型:“post”,
数据类型:“json”,
延误:250,
数据:函数(参数){
返回{
q:params.term,//搜索项
};
},
processResults:函数(数据){
返回{结果:数据};
},
缓存:真
},
最小输入长度:3,
});
我的html代码

<select name='item' class=\"js-productList form-control\" required></select>

productList.php中的代码

    <?php 
// connect to database 
require('../../config/koneksi.php');

$q = $_POST['q'];

$sql = mysql_query("select id_item as id, item_name as text, item_code as code from item where item_name like '%".$q."%'");
$num = mysql_num_rows($sql);
if($num > 0){

    while($data = mysql_fetch_assoc($sql)){
        //I want to show the item_name and item_code
        $tmp[] = $data;

    }
} else $tmp = array();

echo json_encode($tmp);

?>

类似这样的东西。似乎是典型的解析json。 我更喜欢使用$。post:

 <select name='item' id="itemselect" class=\"js-productList form-control\" required></select>

var data = {
q: params.term // or the way you obtain them
}
$.post('halaman/hal_search2ajax/productsList.php', data, function(response) {
$.each(response,function(i,item){
$('#itemselect').append($('<option>', { 
    value: item.code, 
    text : item.code 
}));
}
}, 'json');

风险值数据={
q:params.term//或获取它们的方式
}
$.post('halaman/hal_search2ajax/productsList.php',数据,函数(响应){
$。每个(响应、功能(i、项目){
$('#itemselect')。追加($('',{
值:item.code,
文本:item.code
}));
}
}“json”);
使用append了解值和文本中的内容

<option value="'+value+'">'+text+'</option>
“+text+”

您只需要使用map()函数操作ajax调用返回的响应

像这样试试

$(".js-productList").select2({
  ajax: {
    url: "halaman/hal_search2ajax/productsList.php",
    type: "post",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
      };
    },
    processResults: function(data){
        output = $.map(data, function (obj) {
                        obj.text = obj.text + "-" + obj.code
                        return obj;
                    });
        return { results: output };
      },
    cache: true
  },
  minimumInputLength: 3,
});
下面是一个工作示例


我希望它能对您有所帮助。

我想显示更多文本“+text1+”-“+text2+”,但您可以添加文本:item.code、text2:item.text、text3:item.id