无法单击要接受的按钮 [英] Can't get onclick on a button to be accepted

查看:78
本文介绍了无法单击要接受的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前具有以下形式的链接:

I currently have a link in the below form:

<a href="javascript:changeNumbers('Numbers', '@Url.Action("ChangeNumbers")')">Change</a>

为了适合我要添加此链接的网站的外观,我想将其更改为按钮输入,如下所示:

In order to fit the look of the site in which I'm adding this link, I want to change it to a button input, as so:

<input type="button" value="Change" onclick="changeNumbers('Numbers', '@Url.Action("ChangeNumbers")')" />

但是,我遇到了第二种形式的麻烦:@Url.Action("ChangeNumbers")周围的单引号被标记为Unterminated string constant.谁能告诉我我做错了什么以及如何解决?

However, I'm running into a snag with this second form: the single quotes around @Url.Action("ChangeNumbers") are being flagged as Unterminated string constant. Can anyone tell me what I'm doing incorrectly and how to fix it?

编辑

我没有尝试过该页面-看起来第二种形式有效.所以现在我的问题是-为什么Visual Studio将此标记为不正确?

It didn't occur to me to just try the page - it looks like the second form works. So now my question is - why is Visual Studio flagging this as incorrect?

推荐答案

您本质上并不是在错误地"做任何事情,只是Razor并不完美,而且引号中的引号会导致其异常

You're not doing anything "incorrectly" per se, it's just that Razor isn't perfect, and things like quotes within quotes tend to cause it to freak.

一种快速的解决方法是将URL存储在一个变量中,然后使用该变量:

One quick fix would be to store the URL in a variable and then use the variable:

@{ var url = Url.Action("ChangeNumbers"); }
<input type="button" value="Change" onclick="changeNumbers('Numbers', '@url')" />

但是,更好的解决方案是根本不使用onclick属性.将其放在所属的位置:在JS中.

However, an even better fix is to not use the onclick attribute at all. Put this where it belongs: in JS.

<script>
    $('#myButton').on('click', function () {
        changeNumbers('Numbers', '@Url.Action("ChangeNumbers")');
    });
</script>

使用了上面的jQuery,因为它默认包含在MVC中

这篇关于无法单击要接受的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆