【问题标题】:Limit to One When the Checkbox is Checked选中复选框时限制为一个
【发布时间】:2013-07-17 05:40:57
【问题描述】:

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

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

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

谢谢

【问题讨论】:

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


    【解决方案1】:

    jquery:

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

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

    或javascript:

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

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2021-12-23
      相关资源
      最近更新 更多