我有一个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调用控制器操作?
答案 0 :(得分:3)
GSP正在服务器端,客户端的JavaScript。你不能混合它,使用ajax:
$.ajax({
type: 'POST',
url: "${createLink(action:'delete', controller:'book')}/" + checkboxes[i].id
});
答案 1 :(得分:3)
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");
}
如果有任何错误,请通知我
答案 2 :(得分:0)
remoteFunction
很好,但是你需要一个javascript库。查看http://grails.org/plugin/jquery以及如何安装它。
答案 3 :(得分:0)
如果您编写内联javascript,则可以执行此操作。但是,如果javascript位于您包含的文件中 - 它将无法正常工作。你也不需要“g”。在remoteFunction。