我有一个页面,其中有几十个(动态变化的数量)输入字段(主要是文本,几个按钮),当表单完成/提交/完成时,它不能再编辑,但仍然可以查看。我所做的是声明一个名为disabled的字符串,如果表单已经完成,则将其设置为
disabled="disabled="disabled""
,否则为空。然后我将它放在每个输入标记中,这样它们将被禁用。这很好。但是,光标仍然变为按钮上的指针和文本框上的文本光标。我怎样才能改变这一点呢?我正在尝试
string disabled = (bool)ViewData["Finalized"] ? "disabled = "disabled" style="cursor:default"" : null;
将标签中的引号显示为"。禁用功能可以正常工作,但光标不受影响。我试过在字符串内用单引号重写它(相同的结果),并在字符串周围用单引号和双引号(错误,文字中字符太多),但没有成功。这件事可以这样做吗?还是我用错了方法?
以上设置的输出结果是<input maxlength="4" type="text" name="input1" value="1" disabled = "disabled" style="cursor:default"/>
忘记了,输入是在页面上按照以下示例定义的:
<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%: disabled %> />
那么输出就是这样吗?
<input maxlength="4" type="text" name="input1" value="1" disabled = "disabled" style="cursor:default"/>
你禁用的字符串得到html编码时,它设置在元素上。原因在于你将它设置到元素上的方式。
改变:
<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%: disabled %> />
:
<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%= disabled %> />
…你应该做得很好。原因是<%: disabled %> html编码,而<%= disabled %>不编码。
当您在视图中呈现任何服务器变量和标记时,您应该使用MvcHtmlString.Create
方法。它在渲染时不会对特殊字符进行编码。试试这个,它会解决你的问题。
<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%= MvcHtmlString.Create(disabled) %> />
当您不希望光标显示时,请尝试使用cursor:none
尝试添加一些东西到您的禁用字符串:
onfocus="javascript:if (this.disabled) this.style.cursor = 'none';
您也可以尝试将输入设置为只读,同时禁用它。我不知道这是否会影响光标,但可能会。
如果有疑问,请使用jQuery!
function hideCursor() {
$("input[disabled='disabled']").css("cursor","default");
}
随时调用它。
也是为了使生活更简单:
function disableForm() {
$("#myForm input").attr('disabled', 'disabled');
$("#myForm input").css("cursor","default");
}
干杯!
如何将此添加到CSS中,而不是尝试在每个元素上生成样式?
input[disabled]
{
cursor: default;
}
(注意,在我的测试中,Chrome已经在禁用输入上使用默认光标)