我的GSP文件中有一个选择标签
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
我希望最初选择ID为2的客户端(在简单的html中,它是通过使用selected =“selected”实现的)
我该怎么做?
最佳答案
您需要在此标记中指定value属性。 http://grails.org/doc/2.0.x/ref/Tags/select.html
所以在你的例子中
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
这里要注意的一件事是,您选择的值必须是等于列表中项目的对象,而不仅仅是id-这是很多人被绊倒的地方。因此,您不能只说value ='2',而是需要在from属性中的列表中指定对象。
从文档-
value (optional) - The current selected value that evaluates equals() to true for one of the elements in the from list.
关于grails - GSP-选择标签。如何实现选定的=“selected”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8816319/