Javascript 更新Html.TextBoxFor";只读“;属性打开和关闭
我在抱怨这个, 在MVC上工作,在Html.TextBoxFor值发生更改时,我想更改另一个Html.TextBoxFor的只读属性。 我尝试了几种使用IE的方法,但都不起作用。 下面是一些代码:Javascript 更新Html.TextBoxFor";只读“;属性打开和关闭,javascript,jquery,html,asp.net-mvc,readonly,Javascript,Jquery,Html,Asp.net Mvc,Readonly,我在抱怨这个, 在MVC上工作,在Html.TextBoxFor值发生更改时,我想更改另一个Html.TextBoxFor的只读属性。 我尝试了几种使用IE的方法,但都不起作用。 下面是一些代码: @Html.TextBoxFor(x => x.NumberOfQuestion, new { id = "ExamOptions", onchange = "javascript:ReadOnlyHandle();" }) <script> function ReadOn
@Html.TextBoxFor(x => x.NumberOfQuestion, new { id = "ExamOptions", onchange = "javascript:ReadOnlyHandle();" })
<script>
function ReadOnlyHandle() {
//document.getElementById("NoDiffNum").readOnly = true;
var startHSelect = document.getElementById("NoDiffNum");
debugger;
startHSelect.setAttribute("readonly", "true");
}
debugger;
</script>
@Html.TextBoxFor(x=>x.NumberOfQuestion,新的{id=“ExamOptions”,onchange=“javascript:ReadOnlyHandle();”)
函数ReadOnlyHandle(){
//document.getElementById(“NoDiffNum”).readOnly=true;
var startHSelect=document.getElementById(“NoDiffNum”);
调试器;
setAttribute(“只读”、“真”);
}
调试器;
表中我要更改的行:
<td id="NoDiffNum">@Html.TextBoxFor(model => model.Topics[i].NumberOfNoDifficulltySet)</td>
@Html.TextBoxFor(model=>model.Topics[i].numberOfNodeDifficultySet)
只读属性未更改-
欢迎任何帮助
文档。getElementById(“NoDiffNum”)
指的是
元素,而不是文本框。您正在td上设置只读
您需要针对文本框,而不是td
,因此将id移动到文本框中
<td>@Html.TextBoxFor(model => model.Topics[i].NumberOfNoDifficulltySet, new { id="NoDiffNum" })</td>
在函数中使用jQuery
function ReadOnlyHandle() {
$("#NoDiffNum").prop("readonly", true);
}
如果我们使用jQuery,我们可以删除内联onchange
@Html.TextBoxFor(x => x.NumberOfQuestion, new { id = "ExamOptions" })
我们设置了一个处理器
$(document).ready(function() {
$("#ExamOptions").on("change", function(event) {
ReadOnlyHandle();
});
});
编辑
基于TextBoxFor元素值中的值,我希望另一个TextBoxFor元素更改其只读属性的开和关<代码>@Html.TextBoxFor(x=>x.NumberOfQuestion,new{id=“ExamOptions”,onchange=“javascript:ReadOnlyHandle();”})@*@Html.EditorFor(model=>model.NumberOfQuestion)*@@Html.ValidationMessageFor(model=>model.NumberOfQuestion) 从您的评论来看,现在不太清楚您想要设置为只读。但在任何情况下,都需要在更改处理程序中调整目标id
var targetId = "NumberOfQuestion";
$("#" + targetId).prop("readonly", true);
您是否如标签所示使用jQuery?您是否尝试过在输入上而不是在TD上设置readonly属性?抱歉,Sam,我不理解您的建议。看起来脚本执行了,但属性没有改变-我尝试过使用它-我缺少什么?
document.getElementById(“NoDiffNum”)
指的是
元素,而不是文本框。您正在td上设置readonly。请注意,该标记用于询问有关模式的问题,而您的问题是关于ASP.NET特定的实现,您应该使用该标记。我将再次尝试解释我需要什么。。。由于此解决方案不适用于我…基于TextBoxFor元素值中的值,我希望另一个TextBoxFor元素更改其只读属性的开和关@Html.TextBoxFor(x=>x.NumberOfQuestion,new{id=“ExamOptions”,onchange=“javascript:ReadOnlyHandle();”})@*@Html.EditorFor(model=>model.NumberOfQuestion)*@@Html.ValidationMessageFor(model=>model.NumberOfQuestion)
var targetId = "NumberOfQuestion";
$("#" + targetId).prop("readonly", true);