我有一个包含几十个(动态可变量)输入字段的页面(主要是文本,几个按钮),当表单完成/提交/完成时,它不能再被编辑但仍然可以查看。我要做的是声明一个名为disabled的字符串,如果表单已经完成,则将其设置为
disabled="disabled=\"disabled\""
,否则为null。然后我将它放在evey输入标签中,以便它们被禁用。这很好用。但是,光标仍然会更改为按钮上的指针和文本框上的文本光标。我怎么能改变呢?我正在尝试
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 %> />
答案 0 :(得分:1)
当您在视图中呈现任何服务器变量以及标记时,您应该使用MvcHtmlString.Create
方法。它在呈现时不会对特殊字符进行编码。试试这个会解决你的问题。
<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%= MvcHtmlString.Create(disabled) %> />
答案 1 :(得分:1)
所以输出就是这个?
<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 %> />
......你应该好。原因是&lt;%:禁用%&gt; html编码和&lt;%=禁用%&gt;没有。
答案 2 :(得分:0)
如果您不想显示光标,请尝试使用 cursor:none 。
尝试在禁用的字符串中添加此效果:
onfocus="javascript:if (this.disabled) this.style.cursor = 'none';
答案 3 :(得分:0)
您也可以尝试将输入设置为只读,同时禁用它。我不知道这是否会影响光标,但可能会。绝对比jQuery或JS解决方案简单。
答案 4 :(得分:0)
如有疑问,请使用jQuery!
function hideCursor() {
$("input[disabled='disabled']").css("cursor","default");
}
随时随地打电话。
还要简化生活:
function disableForm() {
$("#myForm input").attr('disabled', 'disabled');
$("#myForm input").css("cursor","default");
}
干杯!
答案 5 :(得分:0)
如何将此添加到CSS而不是尝试在每个元素上生成样式?
input[disabled]
{
cursor: default;
}
(请注意,在我的测试中,Chrome已经在已禁用的输入上使用默认光标)