选中复选框时限制为一

时间:2013-07-18 10:14:41

标签: javascript jquery asp.net-mvc drop-down-menu checkbox

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

enter image description here

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

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编写的验证,但我不知道如何开始:(

由于

1 个答案:

答案 0 :(得分:0)

Jquery的:

if($('#chkWorkerIsPrimary').is(':checked')){
   //Your code    
}
  

.is(':checked')如果用户选中了复选框,则返回true,否则返回false

或javascript:

if ($("[name=chkWorkerIsPrimary]:checked").length == 0){
    //your code
}