未调用 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
Html.ActionLink onclick JavaScript function is not invoked and name of function is corrupted in output HTML
提问by Yoda
I looked at other question: how to call javascript function in html.actionlink in asp.net mvc?showing how to add onclick
to @Html.ActionLink
but in my case function I assigned to onclick
is 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&onclick=alert%28a%29%3B">Call Cell Phone</a></p>
<p> <a class="btn btn-default" href="/Person/Call/7?number=98873213&onclick=Call%28%29%3B">Call Client's Secondary Number »</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 Attributes
block 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>