Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 使用主干设置下拉列表的默认选择_Backbone.js - Fatal编程技术网

Backbone.js 使用主干设置下拉列表的默认选择

Backbone.js 使用主干设置下拉列表的默认选择,backbone.js,Backbone.js,我的问题与之非常相似,只是有一个细微的区别,我不想通过jQuery操作DOM 为此,我所做的是:- 假设我的下拉列表类型为UserType var userType = backbone.Model.extend({ idAttribute: "TypeId", defaults: { TypeId: null, TypeName: null, Selected: false } }); 在这里,我创建

我的问题与之非常相似,只是有一个细微的区别,我不想通过jQuery操作DOM

为此,我所做的是:-

假设我的下拉列表类型为
UserType

var userType = backbone.Model.extend({
    idAttribute: "TypeId",
    defaults: {
        TypeId: null,
        TypeName: null,
        Selected: false
    }
});
在这里,我创建了一个额外的属性作为
Selected
,我正在把手模板中使用它,如下所示

<select id="userType" name="UserTypeId" class="input-medium form-control">
    {{#each MasterUserType}}
    <option id="{{TypeId}}" selected="{{Selected}}">{{TypeName}}</option>
    {{/each}}
</select>
现在,我操纵我的变量
MasterUserType
,使其如下所示:-

[
   {
      "TypeId":1,
      "TypeName":"Admin",
      "Selected":""
   },
   {
      "TypeId":2,
      "TypeName":"User",
      "Selected":"selected"
   },
   {
      "TypeId":3,
      "TypeName":"Super Admin",
      "Selected":""
   }
]
因此,我的HTML呈现为:-

<div class="controls">
    <select id="userType" name="TypeId" class="input-medium form-control">

        <option id="" selected="">Admin</option>

        <option id="" selected="selected">User</option>
        <option id="" selected="">Super Admin</option>


    </select>
</div>

管理
使用者
超级管理员
但它仍然反映了第一项

谁能告诉我怎么了?或者其他选择

最后,我们能否从其他更好的方式来实现这一要求


我已尝试将“selected with true”替换为“selected with true”,但无效。

请尝试按如下方式更新模板:

<select id="userType" name="UserTypeId" class="input-medium form-control">
      {{#each MasterUserType}}
        {{#if Selected}}
            <option id="{{TypeId}}" selected="{{Selected}}">{{TypeName}}</option>
        {{else}}
            <option id="{{TypeId}}">{{TypeName}}</option>
        {{/if}}   
      {{/each}}
</select>

{{{#每个MasterUserType}
{{#如果选中}
{{TypeName}}
{{else}
{{TypeName}}
{{/if}
{{/每个}}

你能帮我做些其他选择吗?由于它在这种情况下有效,我已经尝试过了。它在IE7的控制台中抛出错误,称属性“selected”上的
缺少属性值
。。。
<select id="userType" name="UserTypeId" class="input-medium form-control">
      {{#each MasterUserType}}
        {{#if Selected}}
            <option id="{{TypeId}}" selected="{{Selected}}">{{TypeName}}</option>
        {{else}}
            <option id="{{TypeId}}">{{TypeName}}</option>
        {{/if}}   
      {{/each}}
</select>