【问题标题】:Checkbox validation only if this radio button is selectedCheckbox validation only if this radio button is selected
【发布时间】:2015-05-12 18:45:52
【问题描述】:

我正在尝试验证以下单选按钮是否为真,那么必须至少选择一个复选框。

     @using (Html.BeginForm("Print","Home",FormMethod.Post, new{ @class="form", role ="form")
     {
    @Html.AntiForgeryToken();
    <div class="col-md-12">
        <div class="alert alert-info">
            If you are multiracial, please complete this item by indicating the ethnic/racial group you identify with most or the ethnic/racial group to which you are usually regarded in the community as belonging.
        </div>
    </div>
    <div class="col-md-9">
        <div class="form-group">
            @Html.LabelFor(model => model.IsMultiracial, "Are you Multiracial?", new { @class = "control-label" })
            @Html.RadioButtonFor(model => model.IsMultiracial, "false", new { onclick = "showRacial(this.value)" }) No
            @Html.RadioButtonFor(model => model.IsMultiracial, "true", new { onclick = "showRacial(this.value)" }) Yes
        </div>
    </div>
</div>
<div class="row">
    <div id="multiracial" style="display: none;">
        <div class="col-md-9">
            <div class="form-group">
                @for (int i = 0; i < Model.ParentRacialGroups.Count(); i++)
                {
                    <input type="hidden" name="ParentRacialGroups.Index" value="@i" />
                    <input type="checkbox" id="ParentRacialGroup" name="ParentRacialGroups[@i].ID" value="@Model.ParentRacialGroups[i].ID" checked="@Model.ParentRacialGroups[i].isChecked" data-size="xs" />
                    <label class="cbx-label small" for="check-2g">@Model.ParentRacialGroups[i].name</label>
                    <br />
                }

            </div>
        </div>
    </div>
</div>
<hr>
<div class="row">
    <div class="col-md-offset-2 col-md-4">
        <div class="form-group">
            <input type="submit" class="btn btn-default" value="Print" />
        </div>
    </div>
</div>
  }

【问题讨论】:

  • 只需在 showRacial 方法中传递对象 'this' 并在方法内部检查 if(this.checked == true) then this.value
  • 表单将如何验证?
  • 你用什么验证?
  • 我的 jquery 中已经有了 showRacial,这不是我需要的帮助
  • 您所显示的只是视图。在您进行验证的位置显示您的控制器代码。您是否向ModelState 添加了错误?您希望如何以及在何处显示错误?

标签: jquery html asp.net asp.net-mvc validation


【解决方案1】:

我不知道这对你是否有用,但你仍然可以尝试这个逻辑来使用 JavaScript 完成你的工作

JSFiddle : Demo

HTML

1 <input type="checkbox" value="1" name="chk" />
2 <input type="checkbox" value="2" name="chk" />
3 <input type="checkbox" value="3" name="chk" />
<br><br>
Radio <input type="radio" value="1" />

JS

var radio = $("input[type='radio']");

radio.click(function() {
var $boxes = $('input[name=chk]:checked');
var num = $boxes.length;

 if(num>0)
 {
    alert(num + " " + "Checkbox selected");
 }
 else
 {
     alert("Not a single Checkbox selected");
 }

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多