2017-01-14 49 views
0

我无法应付这个问题的解决方案。问题内容:用ajax选择内容

select2.full.js:4025 Uncaught TypeError: Cannot read property 'slice' of undefined
at DecoratedClass.HidePlaceholder.removePlaceholder (select2.full.js:4025)
at DecoratedClass.removePlaceholder (select2.full.js:594)
at DecoratedClass.HidePlaceholder.append (select2.full.js:4008)
at DecoratedClass.append (select2.full.js:594)
at Select2. (select2.full.js:1025)
at Select2.Observable.invoke (select2.full.js:651)
at Select2.Observable.trigger (select2.full.js:641)
at Select2.trigger (select2.full.js:5489)
at select2.full.js:5348
at Object.options.transport.self.trigger.message (select2.full.js:3482)

我在做什么错?

$(document).ready(function(){ 
 

 
$('#select2').select2({ 
 
       minimumInputLength:1, 
 
\t \t \t width: '300px', 
 
       placeholder:"Select Parent Menu", 
 
       ajax: { 
 
        type:"get", 
 
        url: "../test/inc/ajax/ajaxlux.php", 
 
        dataType: 'json', 
 
        quietMillis: 100, 
 
        data: function(params) { 
 
         return { 
 
          query: params.term, //search term 
 
          page_limit: 10 // page size \t 
 
\t \t \t \t \t \t \t 
 
         }; 
 
\t \t \t \t \t \t console.log(data); 
 
        }, 
 

 
     results: function(data) { 
 
         var newData = []; 
 
         for (var i = 0; i < data.length; i++) { 
 
          newData.push({ 
 
           id: item.FormID //id part present in data 
 
           , text: item.FormName //string to be displayed 
 
          }); 
 
         } 
 
         return { results: newData }; 
 
        }, 
 

 
       } 
 
      }); 
 
    });
<select id="select2"> 
 

 
</select>';

而且ajaxlux.php:

$sql = " 
SELECT ".$prefix."product.product_id,".$prefix."product.product_name 
FROM ".$prefix."product 
WHERE ".$prefix."product.product_name LIKE '%".$_GET['query']."%' 
AND ".$prefix."product.product_active = '1' 
ORDER BY product_name ASC LIMIT 10 " ; 

    $result= mysqli_query($link,$sql) or die(mysqli_error());  

    $json = []; 
    while($row = mysqli_fetch_assoc($result)){ 
     $json[] = array (
      'FormID' => $row['product_id'], 
      'FormName' => $row['product_name'], 

     ); 
    } 
    echo json_encode($json, JSON_UNESCAPED_UNICODE); 

} 

我通过互联网和相同的代码,我为别人工作,在许多网站看了。

+0

如果问题发生的客户端,它发生的时候? AJAX调用返回之前还是之后?如果是,那么AJAX调用返回什么? – David

+0

当我在Selecta中输入某些内容时会发生这种情况。所以客户端。 – elohard

+0

我不知道阿贾克斯,我只是想绊倒我这一个例子。从我看到的 – elohard

回答

0

我这是怎么了建立矿山得到它的工作:

 $('#select2').select2({ 
      minimumInputLength:1, 
      width: '300px', 
      placeholder:"Select Parent Menu", 
      ajax: { 
       type:"get", 
       url: "../test/inc/ajax/ajaxlux.php", 
       dataType: 'json', 
       quietMillis: 100, 
       data: function(params) { 
        return { 
         query: params.term, //search term 
         page_limit: 10 // page size 

        }; 
        console.log(data); 
       }, 

       processResults: function(data) { 
        newData = $.map(data, function (ind, item) { 
         item.id = item.FormID; 
         item.text = item.FormName; 
         return item; 
        }); 

        return { results: newData }; 
       }, 

      } 
     });