C# 选中该复选框时防止多个主

C# 选中该复选框时防止多个主,c#,javascript,jquery,asp.net-mvc,razor,C#,Javascript,Jquery,Asp.net Mvc,Razor,我有一个表单,当选中复选框时,我想限制下拉列表中的数据 因此,当选中Is Primary复选框时,将不会有2个电子邮件地址表明Is Primary为True!!这就是我想要阻止的 这样,当我保存时,会出现一个消息窗口,让用户知道他/她选中了相同下拉值的复选框 using (Html.BeginForm("Save", "Worker", FormMethod.Post, new { id = "contactForm" })) { <input type="hidden" id="

我有一个表单,当选中复选框时,我想限制下拉列表中的数据

因此,当选中Is Primary复选框时,将不会有2个电子邮件地址表明Is Primary为True!!这就是我想要阻止的

这样,当我保存时,会出现一个消息窗口,让用户知道他/她选中了相同下拉值的复选框

using (Html.BeginForm("Save", "Worker", FormMethod.Post, new { id = "contactForm" }))
{
    <input type="hidden" id="workerId" name="workerId" value="@workerId" />
    <p>
        <label for="ddlContactType">
            Contact Type</label>
        <span>
             @Html.DropDownList("ddlContactType", new SelectList(ViewBag.ContactTypeList, "ID", "Display_Value", workerContactType), "[Please Select]",
                new Dictionary<string, object>
                {
                    {"class","validate[required] inputLong"}
                })
        </span>
    </p>
    <p>
        <label for="txtContactValue">
            Contact Value</label>
        <span>
            <input type="text" id="txtContactValue" name="txtContactValue"  class="validate[required] inputLong" value="" />
            <input type="checkbox" name="chkWorkerIsPrimary" id="chkWorkerIsPrimary" value="true"
                        checked="checked" />
             <label>
                 Is Primary?</label>
        </span>
        </p> 
        <p>
            <span>
               <input type="submit" id="btnAdd" class="styledButton" value="Add" />
            </span>
        </p>
    }
</div>
我需要用Javascript或Jquery编写验证,但我不知道如何开始:


谢谢

这就是你的出发点

$('#chkWorkerIsPrimary :checkbox').click(function() {
    var $this = $(this);  
    if ($this.is(':checked')) {
        // do something
    } 
    else {
     //do something else
    }
});

您可以在复选框的单击事件中处理该问题。回答中的代码我只想验证我是否会限制联系人类型值。我只需要每个下拉列表数据1个IsPrimary,例如,我添加了一个电子邮件地址,并且选中了Is Primary复选框,它将保存。然后,当我再次选中电子邮件地址并选中Is Primary复选框时,会通知电子邮件地址中只有一个IsPrimary