2017-01-14 50 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 }; 
       }, 

      } 
     });