开发者

jQuery template select option

How do I use jQuery templates to select an option?

Let's say my data is: { "color" : "red" }

开发者_如何学编程

I have :

<select>
  <option>blue</option>
  <option>green</option>
  <option>red</option>
</select>

I want red to be the one that's selected by default. How can I do this?

Thanks.


$("select").val(data['color']);


Maybe not the most elegant, but here is a way to do it in your template: http://jsfiddle.net/rniemeyer/z7Uu7/

<select>
    <option {{if color == 'blue'}}selected{{/if}}>blue</option>
    <option {{if color == 'green'}}selected{{/if}}>green</option>
    <option {{if color == 'red'}}selected{{/if}}>red</option>
</select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜