【问题标题】:Onclick remove function issue in GSPGSP 中的 Onclick 删除功能问题
【发布时间】:2015-07-27 14:32:52
【问题描述】:

在 GSP 中,我编写了这样的代码,它将显示文件列表:

       <g:each in="${fileList}" var="file">
            <div>
                <a href="#" onclick="remove('${file.attachmentId}')"> 
                <span class="glyphicon glyphicon-remove"></span></a> 
                <a href="/forms/landing/attachment/${file.attachmentId}" >${file.name}</a> 
                </br>
            </div>
        </g:each>

而我的 JavaScript 代码是:

function remove(attachmentId) {
$(document).ready(function(){
        $('.glyphicon-remove').click ( function(e){
            e.preventDefault();
            $(this).parent().parent().remove();

            $.ajax({
                       url: "${g.createLink(controller: "landing", action: "deleteSelectedFile")}",
                        data: {
                                attachmentId: attachmentId
                        },
                        success: function(data){
                                alert("Success");
                        }

                   });

             });
        });

    }

我正在调用 onclick remove() 函数,将选定的 attachmentId 作为参数传递。第一次只有双击删除文件。

为什么第一次双击后才删除文件?

提前感谢您的帮助。

注意:应用程序在 IE 中运行。

【问题讨论】:

    标签: javascript jquery grails gsp


    【解决方案1】:

    自从这个标签

    <div id="remove">
    

    存在于 g:each 标记内,您在同一页面中创建多个 id。当函数 remove() 被调用时,它会删除它找到“remove”作为 id 的所有 div。使每个 id 唯一,这将解决问题

    由于您使用的是 jQuery,请尝试使用此代码。这将消除唯一 ID 的使用。

    <script>
            $(document).ready(function(){
                $('.glyphicon-remove').click ( function(e){
                    e.preventDefault();
                    $(this).parent().parent().remove();
                });
            });
        </script>
    

    【讨论】:

    • 嗨@elixir,但这里的问题是:让我们考虑我有3个文件。如果我尝试删除第三个文件,第二个文件将被删除。但不是全部。
    • 嗨@elixir 我理解我的基本错误,不应该有多个元素具有相同的ID。但现在我把它当作课堂。如何删除调用 onclick 的特定元素。
    • 嗨@elixir,更新后的解决方案正在取出完整的gsp。现在整个页面被删除。我认为我们正在接近解决方案。但不幸的是,更新后的解决方案对我不起作用。
    • 嗨@elixir 我更新了答案,这非常适合我的要求。感谢您的帮助。
    • 嗨@elixir,该文件已从 UI 中删除,但实际上并未从后端删除!
    【解决方案2】:

    拥有重复的 ID 值会导致任何 DOM(文档对象模型)操作出现问题。您的问题是如何通过浏览器界面从服务器中正确删除文件。 Groovy 可以利用 Apache Ant 进行文件操作,并且通过使用 AntBuilder 类,您可以简化与文件操作相关的各种任务。

    请阅读this entry in the Groovy docs 关于使用 AntBuilder。 并查看 Grails 文档中的服务层以了解如何使用服务。面向服务的体系结构将帮助您保持控制器和域类非常传统,并允许您创建可由多个控制器调用的服务以实现更具体的功能。如果您遵循 Grails 的约定,您的控制器将已经具有 delete 函数。不要在该控制器中添加deleteFile 方法,而是调用定义deleteFile 的服务。

    您要删除的代码如下所示:

    Create a service for file manipulation(例如项目服务部分下的 FileService.groovy)。在这里您可以放置​​类和函数来删除、添加、压缩文本文件。通过调用 AntBuilder 实用程序,利用 groovy 对 ApacheAnt 的内置支持。

    //This is to instantiate an instance of the AntBuilder class.
    import groovy.util.AntBuilder
    
    class FileRemover {
        def ant = new AntBuilder() 
    
    
        //Define the file, which you will want to pass a value from the page. 
        //You may need to tweak the file path to match your project structure. 
    
        def file = new File(ant.project.baseDir,
                        "/forms/landing/attachment/${file.attachmentId}")
        def fileName = file.name.toString()
        assert file.exists()
    
        //Function to remove file
        def fileRemover = file.delete()
    
        //Provide messaging to let the user know the file has been removed
        println 'File ' + {fileName} + ' has been removed.'
    }
    

    此代码需要进行一些个性化更改才能在您的项目中运行,但通过阅读答案中链接的文档并遵循代码的基本思想,您应该能够创建一个删除文件并可以调用的操作来自 GSP 页面。

    更新:

    要查看用 Grails 编写的文件管理器的简单示例,请阅读 Thomas Lin 的 this blog post

    【讨论】:

    • 嗨@Nathan,我遇到了常规代码更改的问题。我无法在前端和后端都取得成功。我们能不能完全靠 JavaScript 自己解决这个问题。
    • 您需要通过服务器端代码从服务器执行删除。在 Grails 中,这可以通过控制器或服务中的 java 代码来实现。通过 javascript 操作 DOM 是客户端操作。我已经更新了我的答案,以链接到 Grails 中有关文件管理的精彩教程。
    【解决方案3】:

    在普惠制中:

    <g:each in="${fileList}" var="file">
                <div>
                    <a href="#" onclick="remove('${file.attachmentId}', this)">
                    <span class="glyphicon glyphicon-remove"></span></a> 
                    <a href="/forms/landing/attachment/${file.attachmentId}" >${file.name}</a> 
                    </br>
                </div>
            </g:each>    
    

    在 JavaScript 中:

    function remove(attachmentId, elem) {
        $(elem).parent().remove();
        $.ajax({
           url: "${g.createLink(controller: "landing", action: "deleteSelectedFile")}",
           data: {attachmentId: attachmentId},
            success: function(data){
                alert("Success");
            }
    
       });
    }
    

    这样就可以完美解决问题了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-27
      • 2019-07-31
      • 2020-11-09
      • 2019-11-07
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      相关资源
      最近更新 更多