为什么操作(在 MVC 4 中 ASP.NET)不与 onclick 事件一起运行

Why doesn't action (in ASP.NET MVC 4) run with onclick event?

本文关键字:不与 onclick 事件 运行 一起 NET ASP 操作 MVC 为什么      更新时间:2023-09-26

我的操作在没有 onclick 事件的情况下运行,但它不会与 onclick 事件一起运行(只有 JavaScript 函数运行良好)。

我在视图中使用它:

@Html.ActionLink(item.Name, "Documents", "Home", New With {.id = item.Id}, New With {.id = item.Id, .onclick="return showMenu("+item.Id+")"})

这是JavaScript函数:

<script type="text/javascript">
function showMenu(id) {
    if (document.getElementById(id).parentNode.getElementsByClassName("subItem")[0] != undefined) {
        if (document.getElementById) {
            thisMenu = document.getElementById(id).parentNode.getElementsByClassName("subItem")[0].style
            if (thisMenu.display == "block") {
                thisMenu.display = "none"
            }
            else {
                thisMenu.display = "block"
            }
            return false
        }
        else {
            return true
        }
    }
    else {
        return false
    }
}

为什么号召性用语没有运行?

如果

showMenu返回 false,则表单将不会发布。

当您在链接(<a>标签)的onclick事件上返回 false 时,您是在告诉浏览器不要跟踪该链接。如果您希望跟踪链接,则应确保返回 true。