我有两个要在单击图像时添加到多重选择列表的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')"/>
当我单击它时,“first-second”被添加到列表中,而不是g:select框的实际值。我也尝试过
${first}
和${second}
,但是没有运气。非常感谢任何帮助,谢谢!
最佳答案
您那里没有任何代码可以检索第一和第二选择列表的值。
您可能需要这样的东西:
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');"/>
关于javascript - Grails:将两个g:select的值添加到HTML多重选择列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3822892/