2011-01-26 1 views
0

に構文エラーが思っていた:jqueryの - 誰もがこれで間違っていたものを私に伝えることができれば、アレイ

function cat_autocomplete(){ 
    var categoryTags = ["boots>black","boots>brown>boys>blue","clothes>small_men>scarfs>blue","boots","boots>brown>boys","boots>brown","ties>casual","clothes","coats","clothes>girls","boots>brown>girls","clothes>small_men>hats","jackets","clothes>mens","clothes>red","boots>brown>red","clothes>small_men>scarfs","shoes","clothes>small_men","ties>smart","ties","clothes>womens"]; 
    $("input[name=category]").autocomplete({ 
     source: categoryTags 
    }); 
} 

放火犯にエラーがcategoryTagsである...私は何も悪いことを参照してください傾けます。

編集:完全なコード

<script type="text/javascript"> 
function cat_autocomplete(){ 
    var categoryTags = ["boots>black","boots>brown>boys>blue","clothes>small_men>scarfs>blue","boots","boots>brown>boys","boots>brown","ties>casual","clothes","coats","clothes>girls","boots>brown>girls","clothes>small_men>hats","jackets","clothes>mens","clothes>red","boots>brown>red","clothes>small_men>scarfs","shoes","clothes>small_men","ties>smart","ties","clothes>womens"]; 
    $("input[name=category]").autocomplete({ 
     source: categoryTags 
    }); 
} 
function prod_autocomplete(){ 
    var productTags = ["clothes>small_men>hats","boots>black","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>mens","clothes>small_men","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats","clothes>small_men>hats"]; 
    $("input[name=product]").autocomplete({ 
     source: productTags 
    }); 
} 
$(document).ready(function(){ 
    prod_autocomplete(); 
    cat_autocomplete(); 
}); 
</script> 

編集:PHPファイル

<?php 
session_start(); 
try{ 
    require_once "../../classes/config.php"; 
    require_once "../../classes/database.php"; 
    require_once "../../classes/categories.php"; 
    require_once "../../classes/global_data.php"; 
    $cOb = new config(); 
    $cOb->set_mode(0); 
    $cOb->set_table_prefix('lazy'); 
    $cOb->dbdetails(array('dbuser' => '', 'dbpass' => '', 'dbserver' => 'localhost', 'dbname' => 'test')); 
    $cOb->set_srv_root('C:/wamp/www/ecom_framework/'); 
    $cOb->set_sec_root('http://localhost/ecom_framework/'); 
    $cOb->set_web_root('http://localhost/ecom_framework/'); 
    $con = Database::getInstance($cOb); 
    ob_start(); 
?> 
<script type="text/javascript"> 
    function cat_autocomplete(){ 
     var categoryTags = [<?php 
      $con = Database::getInstance(); 
      $sql = "SELECT cat_id FROM `" . $cOb::$table_prefix . "_tbl_category` ORDER BY cat_name"; 
      $re = $con->query($sql); 
      $str = ''; 
      while($ob = $re->fetch_object()) { 
       $str .= '"' . categories::get_breadcrum_from_id($ob->cat_id, '>') . '",'; 
      } 
      echo rtrim($str, ',') . "];\n"; 
      ?> 
     $("input[name=category]").autocomplete({ 
      source: categoryTags 
     }); 
    } 
    function prod_autocomplete(){ 
     var productTags = [<?php 
      $con = Database::getInstance(); 
      $sql = "SELECT cat_id, pd_name FROM `" . $cOb::$table_prefix . "_tbl_product` ORDER BY pd_name"; 
      $re = $con->query($sql); 
      $str = ''; 
      while($ob = $re->fetch_object()) { 
       $str .= '"' . categories::get_breadcrum_from_id($ob->cat_id, '>') . '",'; 
      } 
      echo rtrim($str, ','); 
      ?>]; 
     $("input[name=product]").autocomplete({ 
      source: productTags 
     }); 
    } 
    $(document).ready(function(){ 
     prod_autocomplete(); 
     cat_autocomplete(); 
    }); 
    </script> 
    <?php 
    $content = ob_get_clean(); 
    header("Content-type: text/javascript"); 
    echo $content; 
    exit; 
}catch(Exception $err){ 
    die($cOb->mode($err->getMessage()));  
} 
    ?> 
+2

Firebugはエラーを教えてくれません。 – BoltClock

+1

具体的なエラーは何ですか? – zerkms

+0

構文エラー http://localhost/ecom_framework/admin/javascript/autocomplete.js.php Line 3 –

答えて

0

はcategoryTags前にvarキーワードなしでそれを試してみてください。 あなたの問題はJavascriptのスコープだと思います。 categoryTagsを初期化する前にvarキーワードを使用すると、cat_autocompete関数のスコープに属します。 しかし、$()。オートコンプリートはその関数のスコープ内にありません。 $()。オートコンプリートはcategoryTagsをスコープ内で探していて、ウィンドウスコープ内でcategoryTagsを検索しません。 cat_autocomplete関数内のvarキーワードを削除すると、categoryTagsはウィンドウスコープに関連付けられ、$()。autocompleteで利用可能になります。

function cat_autocomplete(){ 
categoryTags = ["foo","bar"]; 
$("input[name=category]").autocomplete({ 
    source: categoryTags 
}); 
} 

上記のすべてに注意してください:グローバルな名前空間を散らすことは、物事に近づける素晴らしい方法ではありません。 しかし、より良い方法を概説することは、この回答の対象外です。 これが役立つことを願っています。

関連する問題