0

我有两个 g:select 组合框,我想在单击图像时将它们添加到多选列表中。

这是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')"/>

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

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

4

1 回答 1

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');"/>
于 2010-09-29T15:12:35.950 回答