gpt4 book ai didi

javascript - 有没有办法从 javascript 函数调用 Controller 操作?

转载 作者:行者123 更新时间:2023-11-29 16:19:36 25 4
gpt4 key购买 nike

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

html代码:

<form name="bookForm" action="list" method="post">
....
<a onclick="deleteBooks();">Delete</a>
....
....
<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代码:

<script type="text/javascript">
function selectAll(){
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;
}
}

function deleteBooks(){
var checkboxes = document.forms['bookForm'].elements['delete_checkbox'];
var counter = 0;
for (i = 0; i < checkboxes.length; i++){
if(checkboxes[i].checked){
counter ++;
${g.remoteFunction(action:'delete', controller:'book', id:checkboxes[i].id) }
}
}
if (counter == 0) alert("select books to delete");
}
</script>

selectAll 函数工作正常,但是 deleteBooks 函数在我添加时导致此错误${g.remoteFunction(action:'delete', controller:'book', id:checkboxes[i].id) }

异常:

Error 500: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;, id: checkboxes[i].id)] on line [26]: Cannot get property &#39;null&#39; on null object
Servlet: grails
URI: /myProject/grails/book/list.dispatch
Exception Message: Cannot get property &#39;null&#39; on null object
Caused by: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;, id: checkboxes[i].id)] on line [26]: Cannot get property &#39;null&#39; on null object
Class: list.gsp
At Line: [26]
Code Snippet:

如果我将其替换为 ${g.remoteFunction(action:'delete', controller:'book') }

异常:

Error 500: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;)] on line [27]: No javascript provider is configured
Servlet: grails
URI: /myProject/grails/book/list.dispatch
Exception Message: No javascript provider is configured
Caused by: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;)] on line [27]: No javascript provider is configured
Class: list.gsp
At Line: [27]
Code Snippet:

如何从 javascript 函数或 jquery 调用 Controller 操作?

最佳答案

function deleteBooks(){
var checkboxes = document.forms['bookForm'].elements['delete_checkbox'];
var counter = 0;
for (i = 0; i < checkboxes.length; i++){
if(checkboxes[i].checked){
counter ++;
var bookId = checkboxes[i].id;
<g:remoteFunction controller="book" action="delete" id="${bookId}" />
}
}
if (counter == 0) alert("select books to delete");
}

如有错误请通知我

关于javascript - 有没有办法从 javascript 函数调用 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11867495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com