2011-04-01 65 views

回答

33

val屬性不起作用,因爲<select>需要從<option> S的有selected屬性的價值。我不是很familar與鬍鬚,不過這應該工作:

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

我認爲你正在使用的模板是不是地道的小鬍子—這太粗粒度;你實際上並不是模板什麼。像這樣的東西可能更小鬍子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 →

+1

謝謝!我不確定這是否是正確的方法。 – nolabel 2011-04-01 21:58:42

+1

沒問題。 TBH,這是我寫過的第一個鬍鬚代碼......但我寫了很多JSP ;-) – 2011-04-01 22:01:29

+0

這是否僅適用於JSON數組?等''選項「:[」val1「,」val2「]' – zygimantus 2017-04-28 06:49:53

2

我有點晚了,我知道,只是以供將來參考:

<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> 
9

,如果你不想要修改數組或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); 
+0

爲了這個工作,你需要刪除'# '從模板中的{{#selected}}'變量部分,否則會拋出「未關閉的部分」錯誤。 – Pere 2015-05-02 12:18:40

+0

是的,謝謝修復。我改變了代碼。 – imperator 2015-05-14 11:56:52

+0

另外,還有一個錯字。在第一行中你定義了'selvar',但是在'selected'函數中,你將它引用爲'selval'。 此外,對於我來說,問題是當'selval'是可變的,而不是你的例子中的「靜態」。我的意思是,當它在'data'對象中時。此外,它是一個數組(想想「多重選擇」)。 – Pere 2015-06-18 11:45:11

2

我使用這個技巧來保持它的簡單:

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)); 

這仍然很容易閱讀!唯一的警告是你不能有'。'在你的價值觀中。

+0

感謝修復@Naktibalda – 2017-10-25 12:59:46

相關問題