問題描述
我正在嘗試在 html 文本框上動態設置 disabled 屬性并遇到問題
I am trying to dynamically set the disabled attribute on the html textbox and having issues
我認為這是我的嘗試:
string disabledString = "";
if (SomeLogic)
{
disabledString = "disabled";
}
Html.Textbox()...new Dictionary<string, object> { { "maxlength", 50 }, { "disabled", readOnlyState } })%>
如您所見,我將 disabled 屬性設置為 "" 或禁用,但是當我測試時,它似乎在任何一種情況下都被禁用.我錯過了什么嗎?
As you can see I am setting the disabled attribute to "" or disabled but when I test, it seems to be disabled in either case. Am I missing something?
推薦答案
這對我們來說很難看,因為這里的 HTML 規范很糟糕.
This was ugly for us, due to the fact that the HTML spec is lousy here.
基本上在我們的視圖代碼中,我們有一些這樣的邏輯:
Basically in our view code we had some logic like this:
bool isPlatformOwner = false;
object disabledAttributes = new { @disabled="disabled", @readonly="readonly" };
//omitted code setting isPlatformOwner
if (isPlatformOwner)
{
disabledAttributes = new { };
}
然后,對于我們的控件,我們有這樣的:
Then, for our controls, we had this:
<%=Html.CheckBoxFor(f => f.AddToReleaseIndicator, disabledAttributes)%>
匿名類型在這里拯救了我們,但是,就像我說的,它有點難看.
Anonymous types saved us here, but, like I said, it got a little ugly.
這篇關于如何在 asp.net-mvc 中的 html 文本框上設置禁用屬性?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!