【问题标题】:mvc 4 How to call javascript function after @Html.RadioButtonFor clickedmvc 4如何在@Html.RadioButtonFor点击后调用javascript函数
【发布时间】:2014-03-10 04:50:21
【问题描述】:

查看有:

<div Monthly @Html.RadioButtonFor(m => m.DDPaymentOption, "Monthly", true) 
     Yearly  @Html.RadioButtonFor(m => m.DDPaymentOption, "Yearly", true)  
</div>

试过

$(":radio").click(function () {

    var Selectedvalue = $(this).val();
    var isDiv = document.getElementById('instalmentScheduleDiv');

    if (Selectedvalue == "Yearly")
    {
        isDiv.className =  "HideDiv";
    }
});

不命中函数

【问题讨论】:

  • 这是一个 jsfiddle 解决方案jsfiddle.net/Tv8EY 现在在您的浏览器中查看,您的 html 看起来如何?与此示例进行比较并解决问题。

标签: javascript asp.net-mvc-4


【解决方案1】:

使用此代码

$('#DDPaymentOption').click(function(){  
         if($(this).val()=='Yearly') 
         {
             $('#instalmentScheduleDiv').addClass('HideDiv');
         }
 });

如果您使用 HideDiv 类来隐藏 div,您可以使用 jquery 方法来显示或隐藏您的 div 元素,例如

$('#instalmentScheduleDiv').hide(); // to hide the element

和/或

    $('#instalmentScheduleDiv').show(); // to show the element

【讨论】:

    【解决方案2】:
    $("input[type=radio]").change(function () {
        var Selectedvalue = $(this).val();
        var isDiv = $("#instalmentScheduleDiv");
        if (Selectedvalue == "Yearly")
        {
            isDiv.hide();
        } else {
           isDiv.show();
        }
    });
    

    【讨论】:

      【解决方案3】:

      所有功劳归于:codingbiz

      你可以这样做。

      function updatePostID(val)
      {
          document.getElementById('PostID').value = val;
      
          //and probably call document.forms[0].submit();
      }
      

      然后为 PostID 设置一个隐藏字段或其他控件

      @Html.Hidden("PostID", Model.addcomment.PostID)
      //OR
      @Html.HiddenFor(model => model.addcomment.PostID)
      

      How do I update a model value in JavaScript in a Razor view?

      【讨论】:

        猜你喜欢
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 2012-06-14
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        相关资源
        最近更新 更多