无法在javascript中设置null的属性'innerHTML'

时间:2016-05-10 12:21:28

标签: javascript jquery

伙计们我有一个包含文本和按钮的列,我想要的是当点击按钮时文本发生变化..这是我的代码

var count = 0;
$(document).ready(function() {
      $("#jqGrid").jqGrid({
        data: data.rows,
        datatype: "local",
        styleUI: "Bootstrap",
        colModel: [

          {
            label: 'Customer ID',
            name: 'CustomerID',
            width: 180,
            editable: true,
            edittype: "custom",
            id: "CustomerID",
            editoptions: {
              custom_element: function(value, options) {
                var parts = value.split(' ');
                var elemStr = '<div><input size="6" id="txt_"' + count + '" value="' + parts[0] +
                  '" /><input type="button" size="5" value="..." onclick="setText();"/></div>';

                count++;

                // return DOM element from jQuery object
                return $(elemStr)[0];
              },
              custom_value: function(elem) {
                var inputs = $("input", $(elem)[0]);
                var first = inputs[0].value;
                return first;
              }
            }
          },

        ],

      });
     });

      function setText() {
        document.getElementById("txt_" + count).innerHTML = "hey";
      }

那么为什么它会给我这个例外? .. PLZ帮助..顺便说一句我是初学者

2 个答案:

答案 0 :(得分:0)

setText中的计数未定义。

第一次更改按钮的onclick功能以传递计数变量

 var elemStr = '<div><input size="6" id="txt_"' + count + '" value="' + parts[0] +
                  '" /><input type="button" size="5" value="..." onclick="setText(' + count + ');"/></div>';

然后接受计数作为参数

function setText(count) {
        document.getElementById("txt_" + count).innerHTML = "hey";
      }

答案 1 :(得分:0)

您可以将计数传递给函数:

var count = 0;
$(document).ready(function() {
      $("#jqGrid").jqGrid({
        data: data.rows,
        datatype: "local",
        styleUI: "Bootstrap",
        colModel: [

          {
            label: 'Customer ID',
            name: 'CustomerID',
            width: 180,
            editable: true,
            edittype: "custom",
            id: "CustomerID",
            editoptions: {
              custom_element: function(value, options) {
                var parts = value.split(' ');
                var elemStr = '<div><input size="6" id="txt_"' + count + '" value="' + parts[0] +
                  '" /><input type="button" size="5" value="..." onclick="setText(' + count + ');"/></div>';

                count++;

                // return DOM element from jQuery object
                return $(elemStr)[0];
              },
              custom_value: function(elem) {
                var inputs = $("input", $(elem)[0]);
                var first = inputs[0].value;
                return first;
              }
            }
          },

        ],

      });
     });

      function setText(count) {
        document.getElementById("txt_" + count).innerHTML = "hey";
      }