【问题标题】:Checkbox validation in mvc3?mvc3中的复选框验证?
【发布时间】:2012-08-09 07:07:40
【问题描述】:

我有一个复选框组控件,我想要求用户至少选中一个框,无论他们检查每一个,还是三个,甚至一个都没有关系。

View code
 ---------
 <% foreach (var item in Model)
             { %>  

                  <%:Html.CheckBox("EmployeId", new { value = item.EmployeeID,@class="required"})%>
                   <span class="field-validation-valid" id="Span1"></span>

                  <%:Html.LabelForModel(item.EmployeeName)%>
                  </div>
                 <%-- <%= Html.CheckBox("Accept", new { @class = "required" })%> 
                  <span class="field-validation-valid" id="Accept_validationMessage"></span>--%> 
          <%} %>                

    </fieldset> 
    <div class="assign_button">

谁能帮我解决这个问题?

【问题讨论】:

  • 那是因为您没有点击您想要接受的答案上的 符号。

标签: asp.net-mvc-3 validation checkbox


【解决方案1】:

请在您想验证它的任何地方调用以下方法。根据您的实现更改 Checkbox 类名称和 div 名称。

function ValidateCheckBoxCheckedOrNot()
    {
        var selectedCheckBoxesValue = '';

        $('#DIVID').find("input:checkbox.CheckBoxClassName:checked").each(function (i, selected) {
                                                            if (selectedCheckBoxesValue.length == 0) {
                                                                selectedCheckBoxesValue += $(selected).val();
                                                            }
                                                            else {
                                                                selectedCheckBoxesValue += ',' + $(selected).val();
                                                            }});
            // Here you also get all the comma separated values if you want else use below method for it
        if(selectedCheckBoxesValue.length == 0)
        {
            alert("Select atleast one checkbox");
        }
    }

或者您也可以使用以下方式进行操作

function ValidateCheckBoxCheckedOrNot()
    {
        var selectedCheckBoxes = $('#DIVID').find("input:checkbox.CheckBoxClassName:checked").length;

        if(selectedCheckBoxes==0)
        {
            alert("Select atleast one checkbox");
        }
    }

编辑后的帖子

<div id="CheckBoxDiv">
<% foreach (var item in Model)
             { %>  

                  <%:Html.CheckBox("EmployeId", new { value = item.EmployeeID,@class="employeeCheckBox"})%>
                   <span class="field-validation-valid" id="Span1"></span>

                  <%:Html.LabelForModel(item.EmployeeName)%>
                  </div>
                 <%-- <%= Html.CheckBox("Accept", new { @class = "required" })%> 
                  <span class="field-validation-valid" id="Accept_validationMessage"></span>--%> 
          <%} %> 
</div>   

<script type="text/javascript">
    //Call following method on your button click event.
    function ValidateCheckBoxCheckedOrNot()
            {
                var selectedCheckBoxes = $('#CheckBoxDiv').find("input:checkbox.employeeCheckBox:checked").length;

                if(selectedCheckBoxes==0)
                {
                    alert("Select atleast one checkbox");
                }
            }
</script>

【讨论】:

  • @RaghavendraBandaru .. 如果它对你有用,请将其标记为答案。
【解决方案2】:

您想要客户验证,我说得对吗? 如果你使用 jQuery,断言这个集合

  $('input[type="checkbox"].required:checked');

大于零。

我的意思是这样的:

function validateCheckbox(){
    var count = 0;
    $('input[type="checkbox"].required:checked').each(function() { ++count; } );
    return count > 0;
}

你有按钮&lt;div class="assign_button"&gt;。在这种情况下,您可以编写:

$(function(){
    $('div.assign_button').click(function(){
            if (!validateCheckbox()) {
                alert('Please, check something!');
            }
            else {
                alert('You are wonderful!');
            }
        });
});

【讨论】:

  • @Kipwoker...请在 jquery 中给我详细信息
  • 函数 validateCheckbox(){ var count = 0; $('input[type="checkbox"].required:checked').each(function() { ++count; } );返回计数> 0; }
猜你喜欢
  • 2011-09-20
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多