0

在页面上有一个 CheckBoxList。我创建了这个:

@foreach (var project in Model.Projects)
{
    <input type="checkbox" 
        id="@project.Name"
        name="Projects"
        value="@project.ID"
        title="@project.Name" />
    <label for="@project.Name">@project.Name</label><br/>
}

如何限制选定复选框的最大数量?

4

1 回答 1

0

尝试这个:

 int count = 1;
 int maxnoofselected = 1;

@foreach (var project in Model.Projects)
{
    if(count <= maxnoofselected)
    {
        <input type="checkbox" 
        id="@project.Name"
        name="Projects"
        value="@project.ID"
        title="@project.Name"
        checked="checked" />
        <label for="@project.Name">@project.Name</label><br/>
        @count++;
    }
    else
    {    
        <input type="checkbox" 
        id="@project.Name"
        name="Projects"
        value="@project.ID"
        title="@project.Name" />
        <label for="@project.Name">@project.Name</label><br/>
    }
}

如果 count 小于或等于 maxnoofselected(即您的数字),则复选框将被选中,否则它将被取消选中。

于 2013-07-31T07:28:22.963 回答