Backbone.js templating examples

后端 未结 1 694
无人及你
无人及你 2021-01-29 05:12

How to add radio button as an attribute to model and view it in a template.


 

        
相关标签:
1条回答
  • 2021-01-29 05:36

    If I got it right you are interested in checking the right radio button on some attributes in the model.

    In this case your template should look like somewhat:

    <script type="text/template" id="radio-template">
        <input type="radio" <% f == "opt1" ? print("checked") :'' %> >
        <input type="radio" <% f == "opt2" ? print("checked") :'' %> >
        ...
    </script>
    

    where f is the template variable you specified during the view setup, like:

    var template = _.template($('#item-template').html(), {f: 'opt2'});
    this.$el.html(template);
    

    Not that you can either use only checked or checked="true"

    EDIT

    Then your template should look like:

    <script type="text/template">
        <label>Age:</label> <input type="text" name="age" value="<%= age %>">
        <label>Radio:</label> <input type="radio" value="<%= modelAttribute%>">
    </script>
    
    0 讨论(0)
提交回复
热议问题