简体   繁体   English

jqGrid和jquery click事件的问题

[英]Issue with jqGrid and jquery click event

I'm loading a jqGrid on my page. 我正在页面上加载jqGrid。 The grid has a Delete button for each row. 网格每行都有一个“删除”按钮。 I'm trying to use the jquery UI dialog confirmation on my Delete button. 我正在尝试在“删除”按钮上使用jquery UI对话框确认。

Here's my javascript code: 这是我的javascript代码:

<script type="text/javascript">

    $(document).ready(function () {

        $("#list").jqGrid({
            url: '/MyController/MyFunction/',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['', 'Name', ''],
            colModel: [
                { name: 'Edit', index: 'Edit', width: 40, align: 'left', sortable: false },
                { name: 'Name', index: 'Name', width: 120, align: 'left' },
                { name: 'Delete', index: 'Delete', width: 50, align: 'left', sortable: false }],
            pager: $('#pager'),
            rowNum: 10,
            rowList: [10, 20, 50],
            sortname: 'Name',
            sortorder: "asc",
            viewrecords: true,
            width: 700
        });


        $("#dialog-confirm").dialog({
            autoOpen: false,
            modal: true,
            buttons: {
                "Delete": function () {
                    window.location.href = $(this).attr("href"); ;
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });


        $("a.confirm").click(function () {
            alert("HELLO");
            //$("#dialog-confirm").dialog("open");
        });

    }); 

</script>

I'm passing in data from my controller to the grid. 我正在将数据从控制器传递到网格。 I have the class "confirm" added to the Delete link for each row. 我为每行的“删除”链接添加了“确认”类。

If I click on my Delete button, nothing happens. 如果我单击“删除”按钮,则什么也不会发生。 The link has the correct class, and all my javascript is loading correctly. 链接具有正确的类,并且我的所有JavaScript均正确加载。 I placed an alert at the end of my document.ready function to make sure there were no errors. 我在document.ready函数的末尾放置了一个警报,以确保没有错误。

But if I comment out my jqGrid and add a link onto my page with the class "confirm", the click event will fire. 但是,如果我注释掉jqGrid,并在页面上添加了带有“确认”类的链接,则将触发click事件。

Has anyone ever run into this? 有人遇到过这个吗?

The main problem which you have is that you try to make 'click' binding with $("a.confirm").click(...) before the elements "a.confirm" are loaded. 您遇到的主要问题是,在加载元素“ a.confirm” 之前 ,您尝试使用$("a.confirm").click(...)进行“单击”绑定。

You should either place the binding code inside of loadComplete or gridComplete event handler or use jQuery.live 您应该将绑定代码放在loadCompletegridComplete事件处理程序中,或者使用jQuery.live

$("a.confirm").live('click', function() {
    alert("HELLO");
    //$("#dialog-confirm").dialog("open");
});

instead of $("a.confirm").click(...) . 而不是$("a.confirm").click(...)

One more general remark. 再说一遍。 The best practice working with jqGrid is dividing data from the HTML markup. 使用jqGrid的最佳实践是将数据与HTML标记分开。 I suppose that you place HTML fragment with <a class="confirm">...</a> inside of JSON data returned from the server. 我想您将HTML片段的<a class="confirm">...</a>放在服务器返回的JSON数据中。 jqGrid supports another ways to archive the same results. jqGrid支持另一种存档相同结果的方法。 You can 1) use showlink formatter; 您可以1)使用showlink格式化程序; 2) use custom formatter which allow create any HTML fragment for the grid cell based on the row of data (see rowObject parameter) returned from the server 3) use unobtrusive JavaScript (see my answer with the code example ) 4) any mix from both (see another answer with the code example). 2)使用自定义格式化程序 ,该格式化程序允许根据服务器返回的数据行(请参见rowObject参数)为网格单元格创建任何HTML片段3)使用不引人注目的JavaScript(请参见代码示例的 答案 )4)两者的任何混合(请参见代码示例的另一个答案 )。 The way 3 seems me mostly close to what you do. 方式3似乎让我最接近您所做的事情。

In any way having clear separation of JSON data from HTML markup is good not only because of design reason. 无论如何,将JSON数据与HTML标记清晰地分开是一件好事,这不仅是因为设计原因。 It allows additionally reduce the size of data send from server. 它可以进一步减少从服务器发送的数据大小。 (see this answer for more information) (有关更多信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM