未调用 Html.ActionLink onclick JavaScript 函数且输出 HTML 中的函数名称已损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25703442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:52:42  来源:igfitidea点击:

Html.ActionLink onclick JavaScript function is not invoked and name of function is corrupted in output HTML

javascripthtmlasp.net-mvcrazor

提问by Yoda

I looked at other question: how to call javascript function in html.actionlink in asp.net mvc?showing how to add onclickto @Html.ActionLinkbut in my case function I assigned to onclickis never invoked.

我查看了另一个问题:如何在 asp.net mvc 的 html.actionlink 中调用 javascript 函数?显示如何添加onclick@Html.ActionLink但在我的情况下,我分配给的函数onclick从未被调用。

I tried these action links:

我尝试了这些操作链接:

<p>@Html.ActionLink("Call number", "Call", new { id = Model.Id, number = Model.CellNumber, onclick = "javascript:alert(a);" }, new { @class = "btn btn-default" })</p>
<p> @Html.ActionLink("Call number ?", "Call", new { id = Model.Id, number = Model.SecondaryPhoneNumber, onclick = "javascript:Call();" }, new { @class = "btn btn-default" })</p>

and the function looks like:

该功能如下所示:

<script>
    function Call(){
        alert("BLA");
    }
</script>

but no alert is shown in both cases(first and second button). Besides that action link wroks.

但在两种情况下都没有显示警报(第一个和第二个按钮)。除此之外,操作链接有效。

Question:What I did wrong?

问题:我做错了什么?

EDIT:

编辑:

Action links in html look like that and the look corrupted:onclick=Call%28%29%3B

html 中的操作链接看起来像这样并且外观已损坏:onclick=Call%28%29%3B

<p><a class="btn btn-default" href="/Person/Call/7?number=113-456-789&amp;onclick=alert%28a%29%3B">Call Cell Phone</a></p>
<p> <a class="btn btn-default" href="/Person/Call/7?number=98873213&amp;onclick=Call%28%29%3B">Call Client&#39;s Secondary Number &#187;</a></p>

and the function looks like it should:

该函数看起来应该:

<script>
    function Call(){
        alert("BLA");
    }    
</script>

回答by mykhailovskyi

You add onclick handler in wrong place. You must put it in Html Attributesblock instead Route values. Try it:

您在错误的位置添加了 onclick 处理程序。你必须把它放在Html Attributes块中Route values。试试看:

<p>@Html.ActionLink("Call number", "Call", new { id = Model.Id, number = Model.CellNumber }, new { @class = "btn btn-default", onclick = "alert('a');" })</p>