How do I conditionally specify whether HTML input is disabled/readonly OR NOT? [duplicate]

只愿长相守 提交于 2020-01-06 19:32:12

问题


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
    }
  })

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?


回答1:


HTML :-

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

Try using jQuery as shown :-

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

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


来源:https://stackoverflow.com/questions/29247726/how-do-i-conditionally-specify-whether-html-input-is-disabled-readonly-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!