2011-05-09 72 views
1

我有一个使用编辑器模板的窗体,显示与窗体模型中的子列表有关的一系列复选框。在表单提交时,我需要检查是否所有的复选框都选中了所有子项目,如果不显示模式对话框以确保用户特意决定不选中某些复选框。我知道我可以使用JQuery对话框来显示模式对话框,但是我不知道如何查看编辑器模板中的所有复选框是否已被选中。我的代码是类似如下:如何检查是否选中编辑器模板中的所有复选框

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Test.Models.ModelList>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    ShowPropReturn 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>ShowPropReturn</h2> 

<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script> 
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script> 

<% using (Html.BeginForm()) { %> 
    <%: Html.ValidationSummary(true) %> 
    <fieldset> 
    <legend>ModelList</legend> 

    <div class="editor-label"> 
     <%: Html.LabelFor(model => model.MyProperty1) %> 
    </div> 
    <div class="editor-field"> 
     <%: Html.EditorFor(model => model.MyProperty1) %> 
     <%: Html.ValidationMessageFor(model => model.MyProperty1) %> 
    </div> 

    <div class="editor-label"> 
     <%: Html.LabelFor(model => model.MyProperty2) %> 
    </div> 
    <div class="editor-field"> 
     <%: Html.EditorFor(model => model.MyProperty2) %> 
     <%: Html.ValidationMessageFor(model => model.MyProperty2) %> 
    </div> 

    <table> 
     <%: Html.EditorFor(model => model.MyProperty3) %> 
    </table> 
    <p> 
     <input type="submit" value="Save" /> 
    </p> 
    </fieldset> 
<% } %> 

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

</asp:Content> 

和编辑模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Test.Models.ColumnList>" %> 

<tr> 
    <td> 
    <%= Html.CheckBoxFor(x => x.value) %> 
    </td> 
</tr> 

任何帮助真的会感谢,谢谢。

回答

0

类添加到您的复选框像

<%= Html.CheckBoxFor(x => x.value,new{@class="chk"}) %> 

,然后使用jQuery上提交事件

$("#form").live('submit', function(e){ 
     $(".chk").each(function(){ 
      if($(this).attr("checked") == false) 
      { 
       //show modal dialogue here 
      } 
     }); 
}); 
1

勾CSS类添加到您编辑模板,你要验证的复选框

<%= Html.CheckBoxFor(x => x.value, new { @class = "chk" }) %> 

然后你可以使用jQuery :checked:checkbox选择器做一个size()比较。

if ($('input.chk:checkbox').size() == $('input.chk:checked')) { 

}