获取RadioButton值Razor MVC

我有具有正确和错误值的单选按钮列表。我想获取更改点击事件的值,以便可以将其用于显示条件标签。如果属实;如果标签为假,则我将显示一个标签。它是项目列表,所以我在遍历它。


这是我到目前为止所拥有的:


模型:


public List<bool>answerValues { get; set; }

看法:


<td> 

   @Html.RadioButtonFor(model => model.answerValues[i], true, new { id = 

    "TrueValueID", @onclick = "buttonValue()"}) True


   @Html.RadioButtonFor(model=> model.answerValues[i], false, new { id = 

    "FalseValueID", @onclick ="buttonValue()" }) False 

</td>

的JavaScript


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

 <script>

      function buttonValue(e) {

          alert(e.value);

      }


  </script>

在Controller-Action方法(获取)中,我想要获取true / false值,以便可以基于值显示/隐藏。


我不熟悉Razor / MVC。任何帮助将不胜感激!


繁花如伊
浏览 244回答 3
3回答

慕姐8265434

我希望这能帮到您。剃刀代码在这里,我们可以在单选按钮上设置通用类以绑定js中的change事件。<td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;@Html.RadioButtonFor(model => model.answerValues[i], true, new { id =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "TrueValueID", @class="rdbtnanswervalue"}) True&nbsp; &nbsp; &nbsp; &nbsp;@Html.RadioButtonFor(model=> model.answerValues[i], false, new { id =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "FalseValueID", @class="rdbtnanswervalue" }) False&nbsp;&nbsp; &nbsp; </td>Js代码<script>$(function () {&nbsp; &nbsp; $(".rdbtnanswervalue").on("change", function () {&nbsp; &nbsp; &nbsp; &nbsp; // for current value&nbsp; &nbsp; &nbsp; &nbsp; alert($(this).val());&nbsp; &nbsp; &nbsp; &nbsp; // for all radio button with same class uncomment below code.&nbsp; &nbsp; &nbsp; &nbsp; //$(".rdbtnanswervalue").each(index, ele)&nbsp; &nbsp; &nbsp; &nbsp; //{&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; // get value and perfom action acordingly.&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; alert($(ele).val())&nbsp; &nbsp; &nbsp; &nbsp; //}&nbsp; &nbsp; })})

慕森卡

您应该对所有单选按钮使用name属性。然后在JQuery中$('input:radio[name=RADIO_NAME]').change(function () {&nbsp; &nbsp; &nbsp; &nbsp; var value = $('input:radio[name=RADIO_NAME]:checked').val();&nbsp; &nbsp; &nbsp; &nbsp; switch(value).....&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP