Grails:将两个 g:select 的值添加到 HTML 多选列表

发布于 2024-09-25 09:32:24 字数 838 浏览 6 评论 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} 但没有运气。

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

I have two g:select comboboxes that I want to add to the multiple select list when clicking an image.

Here is the function in 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
            }
            }

I think the problem is here in the actual button:

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

when I click it, "first - second" gets added to the list, not the actual value of the g:select boxes. I also tried ${first} and ${second} but had no luck.

Any help is greatly appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-10-02 09:32:24

那里没有任何代码来检索第一个和第二个选择列表的值。

您可能需要这样的东西:

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

You don't have any code in there that retrieves the values of the first and second select lists.

You would probably need something like this:

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
    // ...
}

You might then use this with the following selects:

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

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

<img ... onclick="addToList('bazList', 'fooList', 'barList');"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文