我收到未捕获的语法错误


  意外的标识符-underscore.min.js 24。


一旦在模板中添加以下复选框代码,就会出现错误:

template: _.template('<h3>'+'<input type=checkbox'+'<% if(status === "complete") print("checked") %> />' + '<% = name %></h3>'),


我猜错误是在模板的复选框代码中,我不知道是什么。

这是我的代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
</head>
<body>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>

</body>

<script>

var Friend = Backbone.Model.extend({});

var friend = new Friend ({ name:'phani'});

var FriendView = Backbone.View.extend({
    el:'body',
    tagName:'article',
    id:'my-friend',
    className:'hello-friend',

    //template
    template: _.template('<h3>'+'<input type=checkbox'+'<% if(status === "complete") print("checked") %> />' + '<% = name %></h3>'),

    //event
    events:{
    "click h3":"alertStatus"
    },

    alertStatus: function(e){
        alert("Hey you clicked the h3!");
        },

    render:function(){
    var attributes = this.model.toJSON();
    $(this.el).html(this.template(attributes));
    }

});

var friendView = new FriendView({model:friend});
friendView.render();
console.log(friendView.el);

</script>

最佳答案

我不确定这是错误的根源,但

template: _.template('<h3>'+'<input type=checkbox'+'<% if(status === "complete") print("checked") %> />' + '<% = name %></h3>'),


您可能应该在“选中”之前留一个空格,否则您将得到类似

<input type=checkboxchecked />


顺便说一句,在checkbox周围也应该有双引号。

09-21 00:05