简体   繁体   中英

Backbone.js templating examples

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

<label>ModemControl:</label>
 <input type="radio" name="modemcontrol" value="0" checked="checked"> off
                        <input type="radio" name="modemcontrol" value="1"> on

i have already completed creating labels,dropdownbox,textbox.i have no idea how to do this can anyone help me?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM