Grails:添加两个g的值:选择一个HTML多选列表

时间:2010-09-29 15:01:08

标签: javascript grails select multiple-select

我有两个g:选择要在单击图像时添加到多选列表的组合框。

这是javascript中的函数:

function addToList(list,firstOpt, secOpt)
            {
            var y = document.createElement('option');
            y.text = firstOpt + ' - ' + secOpt;
            y.value = firstOpt+'-'+secOpt;
            var elSel = document.getElementById(list);
            try {
            elSel.add(y, null); // standards compliant; doesn't work in IE
            }
            catch(ex) {
            elSel.add(y); // IE only
            }
            }

我认为问题出现在实际按钮中:

<img src="${resource(dir:'images',file:'arrow.png')}" onclick="addToList('BList','first','second')"/>

当我点击它时,“first - second”被添加到列表中,而不是g:select框的实际值。我也试过${first}${second},但没有运气。

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

您没有任何代码可以检索第一个和第二个选择列表的值。

你可能需要这样的东西:

function addToList(destinationList, sourceList1Id, sourceList2Id) {

    // your select lists will need ids
    // e.g. <g:select id="listOneId" .../>

    var list1 = document.getElementById(sourceList1Id);
    var list2 = document.getElementById(sourceList2Id);

    var list1value = list1.options[list1.selectedIndex].value;
    var list2value = list2.options[list2.selectedIndex].value;

    // the rest of your addToList() function, replacing 'firstOpt'
    // and 'secondOpt' with 'list1value' and 'list2value' respectively
    // ...
}

然后您可以使用以下选项:

<!-- sources -->
<g:select id="fooList" .../>
<g:select id="barList" .../>

<!-- destination -->
<g:select id="bazList" .../>

<img ... onclick="addToList('bazList', 'fooList', 'barList');"/>