Set the selected item in a select list based on template value

后端 未结 5 1764
生来不讨喜
生来不讨喜 2020-12-14 21:23

How can you correctly pre-populate a select control with the current value from the template?

I have a simple form to edit a record where the values for the selected

5条回答
  •  忘掉有多难
    2020-12-14 21:35

    The selected answer doesn't work anymore. It's an old question but I had a hard time figuring this simple thing out. Here's my solution:

    Handlebars.registerHelper('selected', function(key, value){
           return key == value? {selected:'selected'}: '';
    });
    

    In present version of meteor, we can't set HTML attributes without value, and meteor doesn't allow using {{#if}} either. But the objects passed to the helper are converted into key=value pairs and injected.

    UPDATE

    Use UI.registerHelper instead of Handlebars.registerHelper in new (> 0.8) versions of meteor since Handlebars namespace is depreciated (https://github.com/meteor/meteor/wiki/Using-Blaze#handlebars-namespace-deprecated).

提交回复
热议问题