我正在尝试在asp.net mvc3中使用jquery。我正在使用EditorTemplates作为列表。我的模型是
public class staffinfo
{
public int staffid { get; set; }
public double extgrade { get; set; }
public string lvlid { get; set; }
public IList<SelectListItem> level { get; set; }
public string grdid { get; set; }
public IList<SelectListItem> grade { get; set; }
public double pf { get; set; }
public double wages { get; set; }
public List<hrfacility> facility { get; set; }
}
public class hrfacility
{
public int id { get; set; }
public string name { get; set; }
public double price { get; set; }
public int staffid { get; set; }
public string staffname { get; set; }
public double amount { get; set; }
public bool selected { get; set; }
}
查看:
...
@Html.EditorFor(m => m.wages)
@Html.EditorFor(m=>m.facility)
hrTeacility的EditorTemplate
@model Mysol.Models.hrfacility
...
@Html.DisplayFor(m=>m.name)
@Html.CheckBoxFor(m=>m.selected)
@Html.EditorFor(m => m.staffname)
@Html.EditorFor(m => m.amount)
<script type="text/javascript">
$('#staffname').attr('readonly', true);
</script>
我在editortemplates中使用了没有list部分的上述过程,并且jquery工作正常但是对于这种特殊情况它不起作用!!
是因为可能有多个姓名吗?
如何让jquery为上述情况工作(make staffname readonly)??
P.S。 readonly仅用于示例目的。我还需要使用jquery做其他事情,比如将值赋给文本框。如果我能得到这项工作,我想我也可以做其他事情。
谢谢
答案 0 :(得分:2)
它不起作用,因为staffname
的输入ID应该类似于facility_0_staffname
,facility_1_staffname
等等。
您最好在staffname
输入中添加一个类,然后使用该类访问它。
您可以使用Firebug(在Firefox上)检查ID
答案 1 :(得分:1)
BJ,
我认为你最好的选择可能是class
,而不是Id
,因为你有倍数。你可以尝试:
@Html.TextBoxFor(m => m.staffname, new { @class = "staffname" })
....
<script type="text/javascript">
$(function () {
$('.staffname').attr('readonly', true);
});
</script>
(请注意,您无法使用EditorFor
,因为这不会接受htmlAttributes
,您必须使用基本输入类型,但这并不重要,因为您知道'您想要在只读文本框中显示它)
[编辑] - 当然,如果使用文本框,根本不需要使用类,您当然可以添加样式'inline'来实现相同的结果:< / p>
@Html.TextBoxFor(m => m.staffname, new { @readonly = "true" })
OR(浏览器怪癖):
@Html.TextBoxFor(m => m.staffname, new { @readonly = "readonly" })
<强> N.B 强>
另外还有一件事是,在加载jquery库之前,有一个远程机会(取决于你的javascript加载)实际上正在调用EditorFor模板。通过查看生成的源来检查是不是这种情况。 Prolly不是一个问题,而是一个在编辑器模板和部分内容之前让我感到困惑的问题。