【问题标题】:jquery - syntax error on arrayjquery - 数组上的语法错误
【发布时间】:2011-01-26 06:04:34
【问题描述】:

想知道是否有人能告诉我这有什么问题:

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
    });
} 

firebug 上的错误是 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() ) );    
}
    ?>

【问题讨论】:

标签: javascript jquery arrays syntax-error


【解决方案1】:

尝试在 categoryTags 之前不使用 var 关键字。 我认为你的问题是 Javascript 范围。 通过在初始化 categoryTags 之前使用 var 关键字,它属于 cat_autocompete 函数的范围。 但是, $().autocomplete 不在该函数的范围内。 $().autocomplete 正在其范围内寻找 categoryTags 并且没有找到它在窗口范围内寻找它。 通过删除 cat_autocomplete 函数中的 var 关键字,categoryTags 将绑定到窗口范围并可供 $().autocomplete 使用。

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

对以上所有内容的警告:乱扔全局命名空间并不是处理事情的好方法。 但是,概述更好的方法超出了此答案的范围。 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 2013-05-19
    • 2013-05-23
    • 2010-12-22
    • 2015-05-07
    • 2013-08-19
    • 1970-01-01
    • 2012-09-09
    相关资源
    最近更新 更多