我想知道是否可以通过单击GSP页面中的img来设置g:select值。

<img src="${resource(dir: 'images', file: 'fiorentina.png')}" id = "Fiorentina">

<div class="fieldcontain ${hasErrors(bean: matchDayInstance, field: 'homeTeam1', 'error')} required">
    <label for="homeTeam1">
        <g:message code="matchDay.homeTeam1.label" default="Home Team1" />
        <span class="required-indicator">*</span>
    </label>
    <g:select id="homeTeam1" name="homeTeam1.id" from="${com.baoole.domain.Team.list()}" optionKey="id" required="" value="${matchDayInstance?.homeTeam1?.id}" class="many-to-one"/>
</div>

我试图写一个小脚本来做到这一点,但没有成功:
<script type="text/javascript">
function setOptionValue(event){
    $('#homeTeam1').val(${com.baoole.domain.Team.findByName("Fiorentina")});
}

var img = document.getElementById('Fiorentina');
img.addEventListener("click", setOptionValue);</script>

当我单击图像时,它将重置选择字段。
我认为问题是$('#homeTeam1').val(${com.baoole.domain.Team.findByName("Fiorentina")});

最佳答案

缺少id':

function setOptionValue(event){
  $('#homeTeam1').val( '${com.baoole.domain.Team.findByName("Fiorentina")?.id}' );
}

10-07 14:29