1

年をリストしたドロップダウンリストが1つあります。ここで、すべての行をチェックせずに単一行を使用して値を (モデル値に基づいて) 選択したいと考えています。

このコードは機能しています

<select class="input-small" id="year" name="year">
    <option <%= year == 2013 ? 'selected' : '' %>>2013</option>
    <option <%= year == 2012 ? 'selected' : '' %>>2012</option>
    <option <%= year == 2011 ? 'selected' : '' %>>2011</option>
    <option <%= year == 2010 ? 'selected' : '' %>>2010</option>
    <option <%= year == 2009 ? 'selected' : '' %>>2009</option>
    <option <%= year == 2008 ? 'selected' : '' %>>2008</option>
    <option <%= year == 2007 ? 'selected' : '' %>>2007</option>
    <option <%= year == 2006 ? 'selected' : '' %>>2006</option>
</select>

しかし、コードのすべての行を1行に最適化したかったのです。例えば:

<select class="input-small" id="year" name="year">
    <option>2013</option>
    <option>2012</option>
    <option>2011</option>
    <option>2010</option>
    <option>2009</option>
    <option>2008</option>
    <option>2007</option>
    <option>2006</option>
</select>
<% ('#year').val(year) %> //This is not set my value as per mode 
4

2 に答える 2

0

このjQueryを試してください:

$("#year option:contains('"+year+"')").attr("selected", "selected");
于 2013-06-27T09:39:03.863 に答える