如何有条件地指定HTML输入是禁用还是只读? [英] How do I conditionally specify whether HTML input is disabled/readonly OR NOT?

查看:56
本文介绍了如何有条件地指定HTML输入是禁用还是只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我要实现的目标的一个示例:

Here's a sample of what I'm trying to achieve:

@Html.EditorFor(m => m.Description, 
  new { htmlAttributes = 
    new 
    {
      @class = "form-control",
      @readonly = Model.IsReadOnly,
      disabled = Model.IsDisabled
    }
  })

问题是浏览器会在不检查其内容的情况下对待 readonly disabled 令牌的存在,因此当 IsReadOnly IsDisabled 属性为 false ,它仍将显示为 disabled .

The problem is that the browser treats the existence of the readonly and disabled tokens without checking for their content, so when the IsReadOnly and IsDisabled properties are false, it will still show as disabled.

有没有简单的解决方案?

Is there any simple solution for that?

推荐答案

HTML:-

@Html.EditorFor(m => m.Description, 
  new { htmlAttributes = 
    new 
    {
      @class = "form-control"
    }
});

尝试使用jQuery,如下所示:-

Try using jQuery as shown :-

if(@Json.Encode(Model.IsReadOnly))
{
   $('#Description').attr('readonly','readonly')
}

if(@Json.Encode(Model.IsDisabled))
{
   $('#Description').attr('disabled','disabled')
}

这篇关于如何有条件地指定HTML输入是禁用还是只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆