jquery模板错误$(...)。tmpl不是函数

时间:2013-06-01 11:24:33

标签: jquery asp.net jquery-templates

我想使用jquery模板,但它无法正常工作。 这是我的标记Html。它很简单,但我很困惑为什么它不能正常工作

 <head runat="server">
    <script src="Resource/Scripts/jquery.min.js" type="text/javascript"></script>
        <script src="Resource/Scripts/jquery-tmpl.js"></script>    
            <script id="myTmpl" type="text/x-jquery-tmpl" >
                    <tr>
                        <td><label>${urlPic}</label></td>
                        <td><label>${name}</label></td>
                        <td><label>>${count}</label</td>
                        <td><label>${price}</label></td>
                        <td><label >${sum}</label></td>
                    </tr>
              </script>
            <script type="text/javascript">
                     jQuery(document).ready(function () {
                            var data = [{ urlPic: "abc.jpg", name: "Tom", count: "3", price: "3000000
        0", sum: "40000000" } ];

    // below line raise errro                   
                         $("#myTmpl").tmpl(data).appendTo("#baskettbl2");
                    });
            </script>
  </head>

这是我的HTML

    <body>
    <form id="form1" runat="server">
        <table id="baskettbl2"><table>
</form>
</body>

我收到此错误:

TypeError: $(...).tmpl is not a function

编辑: 我发现它不能仅在第一次请求时起作用。此模板位于我的母版页中。加载默认值时。它不起作用。但是当点击到另一个页面时,它可以工作!有什么问题?我真的很困惑

4 个答案:

答案 0 :(得分:2)

我建议你做2次检查:

  • 使用浏览器内置网络捕获来查看库是否正确加载。
  • 检查页面上的其他库或脚本是否覆盖$ variable。

答案 1 :(得分:1)

我将这些自定义函数(属于使用cookie)包含在我的jquery.js文件中,它覆盖了我的$ char。我评论这些行并解决了错误

    (function (factory) {
    if (typeof define === 'function' && define.amd) {
        // amd. register as anonymous module.
        define(['jquery'], factory);
    } else {
        // browser globals.
        factory(jquery);
    }
}

(function ($) {

    var pluses = /\+/g;

    function raw(s) {
        return s;
    }

    function decoded(s) {
        return decodeuricomponent(s.replace(pluses, ' '));
    }

    function converted(s) {
        if (s.indexof('"') === 0) {
            // this is a quoted cookie as according to rfc2068, unescape
            s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
        }
        try {
            return config.json ? json.parse(s) : s;
        } catch (er) { }
    }

    var config = $.cookie = function (key, value, options) {

        // write
        if (value !== undefined) {
            options = $.extend({}, config.defaults, options);

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new date();
                t.setdate(t.getdate() + days);
            }

            value = config.json ? json.stringify(value) : string(value);

            return (document.cookie = [
                config.raw ? key : encodeuricomponent(key),
                '=',
                config.raw ? value : encodeuricomponent(value),
                options.expires ? '; expires=' + options.expires.toutcstring() : '', // use expires attribute, max-age is not supported by ie
                options.path ? '; path=' + options.path : '',
                options.domain ? '; domain=' + options.domain : '',
                options.secure ? '; secure' : ''
            ].join(''));
        }

        // read
        var decode = config.raw ? raw : decoded;
        var cookies = document.cookie.split('; ');
        var result = key ? undefined : {};
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            var name = decode(parts.shift());
            var cookie = decode(parts.join('='));

            if (key && key === name) {
                result = converted(cookie);
                break;
            }

            if (!key) {
                result[name] = converted(cookie);
            }
        }

        return result;
    };

    config.defaults = {};

    $.removecookie = function (key, options) {
        if ($.cookie(key) !== undefined) {
            // must not alter options, thus extending a fresh object...
            $.cookie(key, '', $.extend({}, options, { expires: -1 }));
            return true;
        }
        return false;
    };

}));

答案 2 :(得分:1)

我遇到了同样的问题,只需检查'jquery.tmpl.min.js'引用必须添加here the link file

答案 3 :(得分:0)

检查$是否被覆盖,在解决方案中搜索noConflict并检查哪个字符覆盖$并使用它。在我的情况下的例子而不是使用$如果我使用_ $ .tmpl()那么它可以工作。

相关问题