如何使用 Mustache.js 在下拉列表中设置选定的值?

发布于 2024-10-29 10:13:21 字数 417 浏览 1 评论 0原文

可以用 Mustache.js 做到这一点吗?

var data = {"val":"3"},
    template = '<select>' +
                    '<option value="1">1</option>' +
                    '<option value="2">2</option>' +
                    '<option value="3">3</option>' +
                '</select>';

var html = Mustache.to_html(template, data);

$(html).appendTo('body');

Is it possible to do this with Mustache.js?

var data = {"val":"3"},
    template = '<select>' +
                    '<option value="1">1</option>' +
                    '<option value="2">2</option>' +
                    '<option value="3">3</option>' +
                '</select>';

var html = Mustache.to_html(template, data);

$(html).appendTo('body');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡水深流 2024-11-05 10:13:21

val 属性不起作用,因为

// snip...        
var html = Mustache.to_html(template, data);
$(html)
    .find('option[value=3]').attr('selected', true)
    .end().appendTo('body');

我认为您使用的模板不是惯用的 Mustache - 它的粒度太粗;你实际上并没有模板化任何东西。像这样的东西可能更符合小胡子的风格:

var template = '<select>{{#options}}' +
                   '<option value="{{val}}" {{#sel}}selected{{/sel}}>' + 
                       '{{txt}}' + 
                   '</option>' + 
               '{{/options}}</select>',

    data = {options: [
        {val: 1, txt: 'uno'},
        {val: 2, txt: 'dos'},
        {val: 3, txt: 'tres', sel: true}
    ]};

var html = Mustache.to_html(template, data);

$(html).appendTo('body');

演示 →

The val attribute doesn't work, because a <select> takes its value from the <option>s which have the selected attribute. I'm not very familar with Mustache, but this should work:

// snip...        
var html = Mustache.to_html(template, data);
$(html)
    .find('option[value=3]').attr('selected', true)
    .end().appendTo('body');

I think that the template you're using is not idiomatic Mustache — it's too coarse grained; you're not actually templating anything. Something like this might be more Mustache-y:

var template = '<select>{{#options}}' +
                   '<option value="{{val}}" {{#sel}}selected{{/sel}}>' + 
                       '{{txt}}' + 
                   '</option>' + 
               '{{/options}}</select>',

    data = {options: [
        {val: 1, txt: 'uno'},
        {val: 2, txt: 'dos'},
        {val: 3, txt: 'tres', sel: true}
    ]};

var html = Mustache.to_html(template, data);

$(html).appendTo('body');

Demo →

太阳公公是暖光 2024-11-05 10:13:21

如果之后不想修改数组或 DOM 树,可以使用函数:

var selval=3; // select 3
var template = '<select>{{#options}}' +
                   '<option value="{{val}}" {{selected}}>{{txt}}</option>' + 
               '{{/options}}</select>';
var data={
    options: [
        {val: 1, txt: 'one'},
        {val: 2, txt: 'two'},
        {val: 3, txt: 'three'}
    ],
    selected: function() {
         if (this.val==selval) return "selected";
         return "";
    }
};


var html = Mustache.to_html(template, data);

if you do not want to modify the array or DOM tree afterwards you can use a function:

var selval=3; // select 3
var template = '<select>{{#options}}' +
                   '<option value="{{val}}" {{selected}}>{{txt}}</option>' + 
               '{{/options}}</select>';
var data={
    options: [
        {val: 1, txt: 'one'},
        {val: 2, txt: 'two'},
        {val: 3, txt: 'three'}
    ],
    selected: function() {
         if (this.val==selval) return "selected";
         return "";
    }
};


var html = Mustache.to_html(template, data);
清醇 2024-11-05 10:13:21

我使用这个技巧来保持简单:

buildOptions = function(object) {
    for (var i in object) {
        object[i + '=' + object[i]] = true;
    }
    return object;
}

通过这种方式,您可以转换这种数据:

{
    field1: 'abc',
    field2: 'xyz' 
}

使用这种胡子模板:

<select name="field1">
    <option {{#field1=abc}}selected{{/field1=abc}}>abc</option>
    <option {{#field1=def}}selected{{/field1=def}}>def</option>
    <option {{#field1=ghi}}selected{{/field1=ghi}}>ghi</option>
</select>
<select name="field2">
    <option {{#field2=uvw}}selected{{/field2=uvw}}>uvw</option>
    <option {{#field2=xyz}}selected{{/field2=xyz}}>xyz</option>
</select>

像这样:

html = Mustache.to_html(template, buildOptions(data));

这仍然很容易阅读!唯一需要注意的是你不能有“.”。在你的价值观中。

I use this hack to keep it simple:

buildOptions = function(object) {
    for (var i in object) {
        object[i + '=' + object[i]] = true;
    }
    return object;
}

In this way, you can transform this kind of data:

{
    field1: 'abc',
    field2: 'xyz' 
}

With this kind of Mustache Template:

<select name="field1">
    <option {{#field1=abc}}selected{{/field1=abc}}>abc</option>
    <option {{#field1=def}}selected{{/field1=def}}>def</option>
    <option {{#field1=ghi}}selected{{/field1=ghi}}>ghi</option>
</select>
<select name="field2">
    <option {{#field2=uvw}}selected{{/field2=uvw}}>uvw</option>
    <option {{#field2=xyz}}selected{{/field2=xyz}}>xyz</option>
</select>

Like this:

html = Mustache.to_html(template, buildOptions(data));

Which still really easy to read! The only caveat is that you can't have a '.' in your values.

无人接听 2024-11-05 10:13:21

我知道,我有点晚了,仅供将来参考:

<script>

var templates = {
    'dropbox': '{{#options}}{{>option}}{{/options}}',
    'option': '<option value="{{value}}"{{#selected}} selected="selected"{{/selected}}>{{text}}</option>'
};

var data = {
    'options': [
        { 'value': '1', 'text': 'One' },
        { 'value': '2', 'text': 'Two', 'selected': true },
        { 'value': '3', 'text': 'Three' }
    ]
};

$('#number').append(Mustache.render(templates.dropbox, data, templates));

</script>

<select id='number' name='number'>
    <option value="0">Choose a number</option>
</select>

I little bit late, I know, just for future reference:

<script>

var templates = {
    'dropbox': '{{#options}}{{>option}}{{/options}}',
    'option': '<option value="{{value}}"{{#selected}} selected="selected"{{/selected}}>{{text}}</option>'
};

var data = {
    'options': [
        { 'value': '1', 'text': 'One' },
        { 'value': '2', 'text': 'Two', 'selected': true },
        { 'value': '3', 'text': 'Three' }
    ]
};

$('#number').append(Mustache.render(templates.dropbox, data, templates));

</script>

<select id='number' name='number'>
    <option value="0">Choose a number</option>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文