简体   繁体   English

如何在@Html.RadioButtonFor 上添加 onclick 事件?

[英]how to add onclick event on @Html.RadioButtonFor?

My radio button basically has a model value.我的单选按钮基本上有一个 model 值。

How can I add an click-eventhandler on @Html.RadioButtonFor ?如何在@Html.RadioButtonFor上添加点击事件处理程序?

@Html.RadioButtonFor(
  model => model.AnswerResponse, 
  true, 
  new { id = "Yes"}, 
  new { onclick = "alert('hi')" 
})

@Html.RadioButtonFor does not contain four arguments. @Html.RadioButtonFor不包含四个 arguments。

if your button is the only one and has a class(or an id by switch. to #), you can do:如果您的按钮是唯一的并且有一个类(或通过 switch.to # 的 id),您可以执行以下操作:

const buttonThatIWantToCall = document.querySelector('.MyElementClass')

This calls the element in a variable.这将调用变量中的元素。 Then, you can verify whenever the element in this variable is called with:然后,您可以验证何时调用此变量中的元素:

buttonThatIWantToCall.addEventListener('click', Function);

then, just do whatever you want to do in the function.然后,在 function 中做任何你想做的事情。

You can pass the onClick handler using the onclick html attribute您可以使用onclick html 属性传递 onClick 处理程序

@Html.RadioButtonFor(
  model => model.AnswerResponse, 
  true, 
  new { @tabIndex = 1, @id = "Yes", @onclick="alert('hi')" }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM