无法在jqGrid中添加按钮单击事件

时间:2017-07-14 13:56:01

标签: javascript jquery jqgrid

我尝试在dialog内点击按钮时添加jqGrid,但收到"Uncaught ReferenceError: OpenDialog is not defined"错误。我想我已经使用了所有需要的参考但没有运气。

此外,如果我在jsFiddle中使用相同的代码,则既不会出现任何错误也不会出现预期的结果。

jsFiddle Demo

我尝试在功能中使用id: "QueryLink",但没有运气。

我在这里缺少什么?



$(function() {
  "use strict";

  function OpenDialog() {
  alert("1");
    $("#myDialogBox").dialog("open");
  }

  $("#grid1").jqGrid({
    colModel: [
      {
        name: "QueryLink",
        id: "QueryLink",
        label: "Query Link",
        align: "center",
        formatter: function() {
          return "<button onclick='OpenDialog()'>Pop Up</button>";
        }
      }
    ],
    data: [{
      QueryLink: "Link"
    }],
    iconSet: "fontAwesome",
    rownumbers: true,
    sortname: "invdate",
    sortorder: "desc",
    caption: "Button Click Test"
  });
});
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.14.0/jquery.jqgrid.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.14.0/css/ui.jqgrid.min.css" rel="stylesheet" />
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.6/js/jquery.jqgrid.min.js"></script>

<table id="grid1"></table>
<div id="myDialogBox" title="Basic dialog"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

首先请注意,您在jQuery之前包含jQueryUI,还有jQuery和jqGrid的多个版本,这会导致代码中出现错误。您需要正确组织包含的脚本。

问题本身是因为您使用的是on*事件属性。这意味着您调用的函数必须在window的范围内声明,因此您需要将OpenDialog()移到$(function() {});块之外。

您还需要在dialog() div上调用#myDialogBox来对该元素上的库进行实例化。大概你想要autoOpen: false,以便它只在你手动触发它时打开。试试这个:

function OpenDialog() {
  alert("1");
  $("#myDialogBox").dialog("open");
}

$(function() {
  "use strict";
  
  $("#myDialogBox").dialog({
    autoOpen: false
  });

  $("#grid1").jqGrid({
    colModel: [{
      name: "QueryLink",
      id: "QueryLink",
      label: "Query Link",
      align: "center",
      formatter: function() {
        return "<button onclick='OpenDialog()'>Pop Up</button>";
      }
    }],
    data: [{
      QueryLink: "Link"
    }],
    iconSet: "fontAwesome",
    rownumbers: true,
    sortname: "invdate",
    sortorder: "desc",
    caption: "Button Click Test"
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.6/js/jquery.jqgrid.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.14.0/css/ui.jqgrid.min.css" rel="stylesheet" />
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css" rel="stylesheet" />

<table id="grid1"></table>
<div id="myDialogBox" title="Basic dialog"></div>

然而,更好的解决方案是删除过时的onclick属性并使用委托事件处理程序,如下所示:

$(function() {
  "use strict";
  
  $("#myDialogBox").dialog({
    autoOpen: false
  });
  
  $("#grid1").jqGrid({
    colModel: [{
      name: "QueryLink",
      id: "QueryLink",
      label: "Query Link",
      align: "center",
      formatter: function() {
        return '<button class="popup-trigger">Pop Up</button>';
      }
    }],
    data: [{
      QueryLink: "Link"
    }],
    iconSet: "fontAwesome",
    rownumbers: true,
    sortname: "invdate",
    sortorder: "desc",
    caption: "Button Click Test"
  }).on('click', '.popup-trigger', function() {
    alert("1");
    $("#myDialogBox").dialog("open");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.6/js/jquery.jqgrid.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.14.0/css/ui.jqgrid.min.css" rel="stylesheet" />
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css" rel="stylesheet" />

<table id="grid1"></table>
<div id="myDialogBox" title="Basic dialog"></div>