2012-08-10 70 views
1

我想在asp.net mvc3中使用jquery。我正在使用EditorTemplates作为列表。我的模型是编辑器中的jquery for

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) 

EditorTemplate为hrfacility

@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使用上述过程,而不该列表部分和jQuery的工作正常,但对于这种特殊的情况,它不工作!

是否因为可能有多个职员姓名?

我怎样才能让jquery为上述情况工作(使staffname只读)?

P.S.只读仅用于示例目的。我需要用jquery做其他事情,比如将值分配给文本框。如果我能得到这份工作,我想我也可以做其他事情。

感谢

回答

2

它不起作用,因为输入的staffname ID应该是这样的facility_0_staffnamefacility_1_staffname等。

您最好在staffname输入中添加一个类并使用该类访问它。

您可以检查与萤火虫的ID(在Firefox)

+0

我们可以在jquery中使用循环来使用所需的文本框吗?如果是这样? – 2012-08-12 03:40:12

+1

@ BJ.BJ:当然,使用[.each()](http://api.jquery.com/each/)。但是,您不需要执行此任务。只要执行'$(“.classname”)。attr('readonly',true)'就可以完成了。 – Mohayemin 2012-08-12 10:47:43

1

BJ,

我觉得你最好的选择可能是在class,工作,而不是一个Id因为你有数倍。你可以尝试:

@Html.TextBoxFor(m => m.staffname, new { @class = "staffname" }) 

....  

<script type="text/javascript"> 
    $(function() { 
     $('.staffname').attr('readonly', true); 
    }); 
</script> 

(注意不能使用EditorFor因为这不会接受htmlAttributes,您必须为你“知道”你想在一个只读文本框来显示它使用基本的inputType,但不会事)

[编辑] - 当然,如果使用的是文本框,就没有必要在所有使用一个类,当然你也可以只添加样式“内联”,以达到相同的结果:

@Html.TextBoxFor(m => m.staffname, new { @readonly = "true" }) 

OR(br) owser怪癖):

@Html.TextBoxFor(m => m.staffname, new { @readonly = "readonly" }) 

NB

一个进一步的东西添加,有小的机会(这取决于你的JavaScript加载)的EditorFor模板实际上是被称为前的jQuery库被加载。通过查看生成的源代码来检查是不是这种情况。这不是一个问题,而是一个使用模板和部分编辑器抓住我的问题。

+0

我已经尝试过这一点,它不工作,要么:(也许是因为使用了列表的,否则没有任何问题的。但为此我需要名单,还有其他方法吗?也许不使用编辑器模板? – 2012-08-10 08:51:04

+0

好吧,如果您使用@ Html.TextBoxFor,您可以将只读attr直接添加到帮助器上,而不是使用javascript。请参阅上面的编辑 – 2012-08-10 08:54:33

+0

不,它仍然不起作用 – 2012-08-10 09:08:18