How to set the selected item in a radio button group in handlebars template?

前端 未结 4 1107
感动是毒
感动是毒 2020-12-19 06:48

In a handlebars template, how do you set a radio button group to the right value using only the template? Can this be done directly in the template?

For an example,

4条回答
  •  悲哀的现实
    2020-12-19 07:32

    My own solution is:

    helper:

    Handlebars.registerHelper ("setRadio", function (value, options) {
        var $el = $(options.fn(this));
        if ( value == $el.val() ) {
           return $el.attr("checked", "checked")[0].outerHTML;
        } else {
           return options.fn(this);
        }
    

    Usage in template:

                {{#setRadio stars}}{{/setRadio}}
                
    
                {{#setRadio stars}}{{/setRadio}}
                
    
                {{#setRadio stars}}{{/setRadio}}
                
    
                {{#setRadio stars}}{{/setRadio}}
                
    
                {{#setRadio stars}}{{/setRadio}}
                
    

提交回复
热议问题