【问题标题】:Errant behavior in the {{each}} tag of jQuery Templates in Internet Explorer?Internet Explorer 中 jQuery 模板的 {{each}} 标记中的错误行为?
【发布时间】:2011-05-07 01:28:08
【问题描述】:

我正在使用 jQuery 模板构建页面,但在 IE 8 中出现了一些不一致的行为。

我在 {{each}} 标记内有一个复选框元素,但是当我尝试将字符串附加到 id 时,IE 会引发错误。

这是 HTML 片段:

<script id="filters-template" type="text/template">
<div id="CoinsuranceFilters" class="filters">
<h4><label><input type="checkbox" id="checkall_coinsurance" onclick="checkClass(this, 'coinsurance_items');" class="checkall" /> Coinsurance</label></h4>
{{each Coinsurance}}
<div class="filteritem">
    <label><input type="checkbox" id="${'Coinsurance_' + id}" {{if IsSelected}} checked{{/if}} class="coinsurance_items"/> ${ItemText}</label>
</div>
{{/each}}
</div>
</script>

IE 报告:“id”未定义

如果我将 id 字段更改为 id="${id}" 错误就会消失(尽管它似乎会为每 1 个复选框呈现 2 个复选框)。

这在 Chrome 和 FF 4 中没有问题

此页面使用 jquery 1.5.2 & jquery-ui 1.8.12

这是传入模板的 JSON 对象:

{
    "IndividualQuoteID": "539b9d05-0c78-4c27-b238-9edaaabfb389",
    "Coinsurance": [
        {
            "id": 267,
            "ID": 267,
            "IsSelected": true,
            "ItemText": "70%"
        },
        {
            "id": 266,
            "ID": 266,
            "IsSelected": true,
            "ItemText": "80%"
        },
        {
            "id": 265,
            "ID": 265,
            "IsSelected": true,
            "ItemText": "90%"
        },
        {
            "id": 264,
            "ID": 264,
            "IsSelected": true,
            "ItemText": "100%"
        }
    ]
}

(id 和 ID 值是故意存在的。我尝试与 {{each}} 块内的字符串连接的任何键都会出现此错误)

【问题讨论】:

    标签: jquery internet-explorer-8 jquery-templates


    【解决方案1】:

    我从未使用过 Jquery 模板,但从我在您的代码中看到的内容不应该是这样的:

    <script id="filters-template" type="text/template">
    <div id="CoinsuranceFilters" class="filters">
    <h4><label><input type="checkbox" id="checkall_coinsurance" onclick="checkClass(this, 'coinsurance_items');" class="checkall" /> Coinsurance</label></h4>
    {{each Coinsurance}}
    <div class="filteritem">
        <label><input type="checkbox" id="Coinsurance_${id}" {{if IsSelected}} checked{{/if}} class="coinsurance_items"/> ${ItemText}</label>
    </div>
    {{/each}}
    </div>
    </script>
    

    我看不出 ${'Coinsurance_' + id} 语法指的是什么。如果我做对了,您希望复选框的 html id 为“Coinsurance_267”。

    【讨论】:

    • 这看起来成功了 - 奇怪的是,这个应用程序的所有地方都使用了语法,这是它唯一有问题的地方(它在其他任何地方都可以工作......不知道为什么语法被使用)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2017-06-26
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多