locked
how to do jquery validation for checkbox in jquery and mvc RRS feed

  • Question

  • User569149469 posted

    Hi,

    I am using editfor for checkbox like this 

       @for (int i = 0; i < Model.Replist.Count; i++)
                                       {
                                           <div class="row">
                                               <div class="col-md-12">
    
                                                   
                                                       @Html.EditorFor(m => Model.Replist[i].ResValue)
                                                       <label class="control-label ">@Model.Replist[i].ResName </label>
                                                       @Html.HiddenFor(m => Model.Replist[i].Id)
                                                   
                                                  
                                               </div>
                                           </div>
                                       }

    I want to get the hidden value id using jquery .I am suing like this 

     $(".check-box").change(function () {            
                var select = "";
                $("input[type = 'checkbox']").each(function () {                
                    var c = $(this).is(":checked");
                    if (c) {
                                alert($("input[type = 'hidden']").value);
                 
                  
                      }
                       
                    } else {
                        $("#txt").val(select)
                    }                
                })                                
            })        

    but no use can any one pls help

    Tuesday, November 20, 2018 4:05 AM

Answers

  • User-271186128 posted

    Hi phmaspnet,

    I am using editfor for checkbox like this 

       @for (int i = 0; i < Model.Replist.Count; i++)
                                       {
                                           <div class="row">
                                               <div class="col-md-12">
    
                                                   
                                                       @Html.EditorFor(m => Model.Replist[i].ResValue)
                                                       <label class="control-label ">@Model.Replist[i].ResName </label>
                                                       @Html.HiddenFor(m => Model.Replist[i].Id)
                                                   
                                                  
                                               </div>
                                           </div>
                                       }

    I want to get the hidden value id using jquery .I am suing like this 

     $(".check-box").change(function () {            
                var select = "";
                $("input[type = 'checkbox']").each(function () {                
                    var c = $(this).is(":checked");
                    if (c) {
                                alert($("input[type = 'hidden']").value);
                 
                  
                      }
                       
                    } else {
                        $("#txt").val(select)
                    }                
                })                                
            })        

    but no use can any one pls help

    According to your code and description, I suppose you just want to get the RepList ID (based on the checked checkbox). If that is the case, I suggest you modify your code as below:

    change the EditorFor to the CheckBox (Note, the value should be bool type); add css class for the CheckBox and HiddenField; the result TextBox is used to display the selected ID.

            <div class="form-group">
                @for (int i = 0; i < Model.Type_Name.Count; i++)
                {
                    <div class="row">
                        <div class="col-md-10">
                            @Html.CheckBoxFor(m => Model.Type_Name[i].IsSelected, new { @class = "check-box" })
                            <label class="control-label ">@Model.Type_Name[i].Name </label>
                            @Html.HiddenFor(m => Model.Type_Name[i].ID, new { @class = "hidden_ID" })
                        </div>
                    </div>
                }
    
                <input id="txtresult" type="text" value="" />
            </div>

    JQuery script:

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".check-box").change(function () {
                var select = [];
    //find all checked checkbox. $("input[type='checkbox']:checked").each(function () {
    //find the hidden field in the save div. then push the value into array. select.push($(this).closest("div").find(".hidden_ID").val()); }); //display the selected value. $("#txtresult").val(select.join()); }); }); </script>

    The screenshot as below:

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2018 2:22 AM