asp.net-mvc 将 OnClick 事件添加到 Html.RadioButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224202/
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
Add OnClick Event to Html.RadioButton
提问by photo_tom
What I want to do is to call a JavaScript routine when the user clicks on a radiobutton. I've some fields to enable/disable when this happens. However, when I enter
我想要做的是在用户单击单选按钮时调用 JavaScript 例程。发生这种情况时,我有一些字段可以启用/禁用。然而,当我进入
<%=Html.RadioButton("obp17", "57", ViewData.Eval("obpValue17").ToString().Equals("57"), new {@onclick = "Yes()"})%>
I'm getting a "type or with" expected errorwhen trying to add the onlick event. This should be easy, but none of samples I've found seem to work. The leading "@" is common in all the examples I've found, but something else seems to be missing.
尝试添加 onlick 事件时,出现“键入或带有”预期错误。这应该很容易,但我发现的样本似乎都不起作用。前导“@”在我发现的所有示例中都很常见,但似乎缺少其他东西。
And yes I know the way of checking for "true" is overkill, but it is created with a special purpose code generator, so it wasn't any extra work.
是的,我知道检查“true”的方法有点矫枉过正,但它是用特殊用途的代码生成器创建的,因此没有任何额外的工作。
Any thoughts?
有什么想法吗?
回答by photo_tom
Pardon me for causing anyone problems. My problem was ultimately caused by delete the first line of the ASCX file that inherits "System.Web.Mvc.ViewUserControl" Once I put that in, all the problems went away.
请原谅我给任何人带来麻烦。我的问题最终是由删除继承“System.Web.Mvc.ViewUserControl”的ASCX文件的第一行引起的,一旦我把它放进去,所有的问题就消失了。
The line of code that worked was
工作的代码行是
<%= Html.TextBox("obpt9",ViewData.Eval("obpt9"), new { onclick = "alert('hi')" })%>
Different line than sample, but they are all the same.
与样品不同的线,但它们都相同。
Again, sorry for causing anyone confusion.
再次,很抱歉让任何人感到困惑。
tom
汤姆
回答by Hedego
The general structure Looks like the following:
一般结构如下所示:
@Html.RadioButtonFor(t => t.ViewModelVariableName,"RadioButtonValue", new {javascriptEvent = "javascriptMethodName"});
回答by tvanfosson
Is the page language VB or C#? If it's VB your syntax is wrong for creating the anonymous typed html attributes. See this MSDN referenceon how to create an object with an anonymous type in VB.
页面语言是VB还是C#?如果是 VB,则创建匿名类型的 html 属性的语法是错误的。请参阅有关如何在 VB 中创建具有匿名类型的对象的MSDN 参考。
<%=Html.RadioButton("obp17",
"57",
ViewData.Eval("obpValue17").ToString().Equals("57"),
New With { .onclick = "Yes()" } ) %>
Or change the page language to C#, if that's more appropriate.
或者将页面语言更改为 C#,如果这样更合适的话。
Also, note that you could (arguably should) simply give the radio button a class, then add all the handlers at one time with jQuery. Usually you want to keep your javascript separate from your mark up.
另外,请注意,您可以(可以说应该)简单地为单选按钮指定一个类,然后使用 jQuery 一次性添加所有处理程序。通常你想让你的 javascript 与你的标记分开。
<%=Html.RadioButton("obp17",
"57",
ViewData.Eval("obpValue17").ToString().Equals("57"),
New With { .class = "heinz" } ) %>
<script type="text/javascript>
$('.heinz').click( function() {
... implement the logic of Yes() here ...
});
</script>
回答by J.W.
Do you have Yes()function correctly defined in your code ? Replace Yes()to the javascript alert()function ; if that works, then the line of code you posted is OK.
你已经Yes()在你的代码功能正确定义?替换Yes()为 javascriptalert()函数;如果可行,那么您发布的代码行就可以了。
Also, please post Yes()function here, and I think there may be some error there.
另外,请Yes()在这里发布功能,我认为那里可能存在一些错误。

