【问题标题】:0x800a138f - JavaScript runtime error: The value of the property ''is null or undefined, not a Function object0x800a138f - JavaScript 运行时错误:属性 '' 的值为 null 或未定义,不是函数对象
【发布时间】:2015-04-23 23:20:12
【问题描述】:

1> 我有剑道网格,其中一列有一个按钮。我已将 javascript 函数分配给按钮的 onclick 事件。 javascript进行ajax调用。但是当我点击按钮时出现错误

0x800a138f - JavaScript 运行时错误:属性“DoSomething”的值为 null 或未定义,不是函数对象

我尝试将 onclick 设置为“javascript:DoSomething();”没有运气

2> 如果可能的话,我还想将“BatchKey”值传递给函数

<div>
    @(Html.Kendo().Grid< BatchDetail>()
    .Name("grid")
    .Columns(col =>
    {
        col.Bound(p => p.BatchKey);
        col.Bound(p => p.OriginalCost);
        col.Bound(p => p.Status);
        col.Bound(p => p.LastFileName);
        col.Bound(p => p.FileID).ClientTemplate(
            "# if (Status == \"Error\") { #" +
            "<button id=\"btnResolve\" class=\"resolve-button\" onclick=\"DoSomething();\">Resolve</button>" +
            "#}#"
            ).Title("Action");
    })
    .AutoBind(true)
    .Pageable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Read(read => read
            .Action("GetData", "Detail", new { ID = Model.ID })))
    )
</div>
function DoSomething()
{   
    $.ajax({
        url: "www.msn.com",
        success: function (result)
        {
            $("#div1").html(result);
        }
    })
}

【问题讨论】:

  • DoSomething 肯定在某些本地范围内。不要将 onclick 属性与字符串一起使用,而是使用 DOM 方法来附加处理程序引用。
  • @user3862378 不要编辑我的帖子,但如果您需要提供更多信息,请添加到您自己的帖子中。或者,只需添加评论。 :)
  • @Vash 我没有看到任何用代码回复的选项。评论的回复选项非常有限。
  • 如果按钮在网格内,并且没有它的 ID。如何访问我在 DoSomething() 函数中单击的按钮。
  • 在您编辑我的答案时,您提到您将函数包装在 $(function () { 块中。还是这样吗?这是我重现它的唯一方法。

标签: javascript jquery kendo-ui kendo-grid kendo-asp.net-mvc


【解决方案1】:

按钮 ID 必须是唯一的,所以不能这样使用。你可以像这样传递 BatchKey:

 col.Bound(p => p.FileID).ClientTemplate(
     "# if (Status == \"Error\") { #" +
         "<button class=\"resolve-button\" onclick=\"DoSomething(this, '#= BatchKey #');\">Resolve</button>" +
      "#}#"
            ).Title("Action");

function DoSomething(button, batchKey)
{   
    $.ajax({
        url: "www.msn.com",
        success: function (result)
        {
            $("#div1").html(result);
        }
    })
}

我已经在自己的网格上对此进行了测试,并且效果很好。如果它仍然不起作用,则说明您发布的代码之外的代码存在问题。

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 1970-01-01
    • 2014-02-22
    • 2013-12-21
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多