【问题标题】:How to disable Html.RadioButtonFor in jquery?如何在 jquery 中禁用 Html.RadioButtonFor?
【发布时间】:2016-09-27 08:13:05
【问题描述】:

这是我的 Html.RadioButtonFor

 <label class="radio-inline">@Html.RadioButtonFor(x => x.Account, Account.A1)<label for="Account"></label>A</label>
 <label class="radio-inline">@Html.RadioButtonFor(x => x.Account, Account.A2)<label for="Account"></label>B</label>

【问题讨论】:

    标签: javascript jquery html asp.net-mvc


    【解决方案1】:

    在 jQuery 中也可以使用 prop

    $('input[name=ticketID]').prop("disabled",true);
    

    您还可以在代码中使用toggleDisabled 作为通用函数。

    (function($) {
        $.fn.toggleDisabled = function(){
            return this.each(function(){
                this.disabled = !this.disabled;
            });
        };
    })(jQuery);
    

    (function($) {
        $.fn.toggleDisabled = function(){
            return this.each(function(){
                this.disabled = !this.disabled;
            });
        };
    })(jQuery);
    $('button').click(function(){
      $('input[name=ticketID]').toggleDisabled();
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" name="ticketID"/>Radio
    <button>Button</button>

    【讨论】:

      【解决方案2】:

      这里不需要使用jquery,可以在RadioButtonFor方法中将值作为html属性传递:

      @Html.RadioButtonFor(x => x.Account, Account.A1, new {disabled = "disabled"} )
      

      【讨论】:

      • 他向 jQuery 老兄询问过。
      • @Loading..: 什么时候可以通过添加简单属性来实现复杂化。
      • 这与复杂性无关,而与问题有关。仅从 Jquery 询问。
      猜你喜欢
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2022-09-26
      • 2014-06-02
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多