我有一个 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:'delete', controller:'book', id: checkboxes[i].id)] on line [26]: Cannot get property 'null' on null object
Servlet: grails
URI: /myProject/grails/book/list.dispatch
Exception Message: Cannot get property 'null' on null object
Caused by: Error evaluating expression [g.remoteFunction(action:'delete', controller:'book', id: checkboxes[i].id)] on line [26]: Cannot get property 'null' 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:'delete', controller:'book')] 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:'delete', controller:'book')] on line [27]: No javascript provider is configured
Class: list.gsp
At Line: [27]
Code Snippet:
如何从 javascript 函数或 jquery 调用控制器操作?