【问题标题】:Uncaught TypeError: Cannot call method 'submit' of null未捕获的类型错误:无法调用 null 的方法“提交”
【发布时间】:2014-01-03 20:40:04
【问题描述】:

Here is a picture of the error. 这个错误是在我对我的项目进行一些更改后出现的。当您按下按钮时,它应该转到用于记录网站用户的控制器。我重新制作了我的布局以引导并更改/删除了很多属于该站点的 CSS。这可能是相关的,但不确定如何。这是按钮的代码:

@if (Request.IsAuthenticated) {
    <text>
        Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
        @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
            @Html.AntiForgeryToken()
            <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
        }
    </text>
} else {
    <ul>
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

有人知道为什么会这样吗?

【问题讨论】:

  • getElementById('logoutForm') 为空,请检查表单是否存在
  • 使用查看源代码验证您的注销表单是否已呈现 id
  • @Marthijn @rene 在新版本中注销似乎是一种形式,这一点很明显:i.imgur.com/hKBdtty.png。在我的旧版本中,我在 css 文件中也有这行代码:#logoutForm { display: inline; }。我应该阅读 CSS 还是什么可能导致表单不存在?
  • 新版本的&lt;form&gt; 缺少id 属性。
  • 在那个线程中,他们说表单中的 JavaScript 不起作用,我对此没有经验。您还可以将&lt;a href.. 更改为&lt;input type="submit" /&gt;,或使用@Khalid Abuhakmeh 下面的jQuery 解决方案

标签: javascript jquery asp.net-mvc twitter-bootstrap


【解决方案1】:

我认为你的问题出在这条线上。

@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {

检查您的页面来源,我希望您的操作在您的表单上看起来像这样。

action="Account/LogOff?id=logoutForm"

解决方案:

@using (Html.BeginForm("LogOff", "Account", null, FormMethod.Post, new { id = "logoutForm" })) {

这应该会为你解决问题:)

更新:

您可以切换代码以使其以不同的方式工作,但仍然可以工作。

选项 1:

将此粘贴​​到布局文件的末尾。

<script>$(function() {
    $('#logoutForm a').on('click', function () {
          $('#logoutForm').submit();
     });
</script>

选项 2:

把标签改成一个

<button type="submit">Log Off</button> <!-- no JavaScript -->

选项 3:

<!-- I think this will work -->
<a onclick="document.getElementById('logoutForm').submit()">Log Off</a>

我个人喜欢选项 2,因为您只是使用了应有的提交按钮,而不是 JavaScript。

更新 #2:

@using(Html.BeginForm("LogOff", "Account", null, FormMethod.Post, new { id = "logoutForm" }) {
    @Html.AntiForgeryToken()
    <button type="submit">Log Out</button>
}

应该产生:

<form action="/Account/LogOff" method="POST" id="logoutForm">
   <input type="hidden" name="__RequestVerification" value="***" />
   <button type="submit">Log Out </button>
</form>

记下 action 和 method 属性。此外,请确保从代码中删除 text 标签,因为它们旨在强制 Razor 语法接受它们之间的任何内容作为文本而不是 Razor 语法。

【讨论】:

  • 您能否使用 chrome 工具“检查页面”并粘贴您为表单标签获取的 html。只是表单标签会很有帮助。
  • 正如我在上面的 cmets 中所说,没有用于注销的表单。您可以在此处查看它的外观(旧版本)和现在的外观(新版本):i.imgur.com/hKBdtty.png。我已经截取了 html 的屏幕截图,因为它现在在表单上:i.imgur.com/lyKmYe8.png。我已经尝试了您发布的所有三种方法,但都没有奏效。唯一给出响应的是方法 2,它刷新站点并给出一个长 URL,其中包含请求验证令牌。其他 2 给出了与之前相同的错误“Uncaught TypeError: Cannot call method 'submit' of null”。
  • 您的表单没有 action="",因此它正在发布到当前页面。你的路由有问题。此外,您的表单没有方法(应该是 POST)。你的代码有 标签吗?
猜你喜欢
  • 2012-09-07
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
相关资源
最近更新 更多