关于 javascript:从 grails 控制器获取复选框值 | 珊瑚贝

Get checkbox value from grails controller


我有一个 grails 项目,我需要在其中选择要删除的字段,当我单击”删除”时,我需要一个函数来删除所有选定的项目:

html代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<form name=“bookForm” action=“list” method=“post”>
    ….
    <g:link controller=“book” action=“delete”>Delete</g:link>
    ….
    ….
    <g:checkBox id=“select_all” name=“select_all” value=“” onclick=“selectAll();” />
    ….
    <g:each in=“${bookList}” status=“i” var=“bookInstance”>
        <tr class=“${(i % 2) == 0 ? ‘odd’ : ‘even’}”>
            <td><g:checkBox id=“${bookInstance.id}” name=“delete_checkbox” value=“” /></td>
        </tr>
    </g:each>
    ….
</form>

javascript 代码:

1
2
3
4
5
6
7
8
9
10
<script type=“text/javascript”>
    function selectAll(){//this function is used to check or uncheck all checkboxes
        var select = document.getElementById(“select_all”);
        var checkboxes = document.forms[‘bookForm’].elements[‘delete_checkbox’];
        if (select.checked){
            for (i = 0; i < checkboxes.length; i++) checkboxes[i].checked = true;
        }else{
            for (i = 0; i < checkboxes.length; i++) checkboxes[i].checked = false;
        }
    }//this function works fine

问题:

我需要检查 gsp 列表中的所有复选框的操作,如果它们被选中,则获取它们的 id 并按 id 删除记录。

我可以通过 groovy 或 javascript 来实现吗?

  • 这是一个 grails 问题,而不是一个 groovy 问题
  • 但是我必须在控制器中编写它来解决我的问题的代码是 groovy


您的代码的问题是您为 checkbox =” 设置了值。这是不正确的,因为值是提交给服务器的内容。

你需要修改如下:

1
        <td><g:checkBox id=“${bookInstance.id}” name=“delete_checkbox” value=“${bookInstance.id}”” /></td>

重写您的复选框,如:

1
<g:checkBox id=“delete_checkbox” name=“delete_checkbox” value=“${bookInstance.id}” />

因此,当您提交时,您将在您的 params 映射中获得一个名为 _delete_checkbox 或 delete_checkbox 的 id 数组。要查看您得到了什么,您可以尝试在操作中打印您的参数。

1
println params

你可以使用params[‘nameOfTheAttribute’]得到想要的数组。
获得 id 数组后,对其进行迭代并将它们全部删除。

  • 当我写 println params.delete_checkbox 或 println params 它返回 null
  • 什么?? println 参数返回空值?奇怪的。如果您创建一个带有值的输入,比如 2,它会显示在参数上吗?
  • 我必须更改 g:link to 按钮才能提交表单。在此更改之后,您的答案是正确的。谢谢Tiago Farias


来源:https://www.codenong.com/11885312/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9280.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?