jeudi 24 août 2017

Get checked checkbox value from view to controller using jQuery

i had the table like this

 <div class="container" id="changeDate">
    <table class="container table table-striped">
        <thead>
            <tr class="label-primary" style="color:white;">
                <th class="col-sm-1"><input id="CAId" name="CAId" type="checkbox" onclick="SetAllCheckBoxes(this)" /></th>
                <th class="col-sm-6">Company Name</th>
                <th class="col-sm-2">DCR Date</th>
                <th class="col-sm-2">AGM Date</th>
                <th class="col-sm-1">Action</th>
            </tr>
        </thead>
@if (comp != null)
        {
            <tbody>
                @foreach (var item in comp)
                {
                    <tr>
                        <td class="col-sm-1"><input id="CId" name="CId" type="checkbox" value="@item.Id" /></td>
                        <td class="col-sm-6">@item.Name</td>
                        <td class="col-sm-2">@item.DCRDate?.ToString("dd MMM yyyy")</td>
                        <td class="col-sm-2">@item.AGMDate?.ToString("dd MMM yyyy")</td>
                        <td class="col-sm-1">
                            <a asp-action="Edit" asp-route-Id="@item.Id"><span class="glyphicon glyphicon-edit"></span></a>
                            <a asp-action="Delete" asp-route-Id="@item.Id" onclick="return confirm('Are you sure to delete @item.Name ?');"><span class="glyphicon glyphicon-remove"></span></a>
                        </td>
                    </tr>
                }
            </tbody>
        }
else
        {
            //TOFIX - DISPLAY when no company found
            <tbody>
                <tr>
                    <td colspan="5">No Record Found!</td>
                </tr>
            </tbody>
        }
    </table>
<div class="container">
        <div class="row form-group pull-right">
            <div class="input-group col-sm-12">
                <span class="input-group-btn" style="width:120px;">
                    @Html.DropDownList("dateList", ViewData["dateList"] as IEnumerable<SelectListItem>, new { @id = "dateList", @class = "form-control" })
                </span>
                <input type="text" class="form-control" readonly="readonly" id="date" name="date" value="" style="width:150px" placeholder="@System.DateTime.Now.ToString("dd-MMM-yyyy")" />
                <button id="dateSubmitBtn" type="submit" class="btn btn-default">Change</button>
            </div>
        </div>
    </div>
</div>
}

i want to know which checkbox was selected by using jQuery. here was the code i tried, but non of it works for me.

if ($('input[name="CId"]').is(':checked')) {}

Or

if ($("#CId").is(':checked')) {}

Or

 var $boxes = $('input[name=CId]:checked');

Or

 if ($('input[name="CId"]:checked')){}

how can i get which checkbox was selected and store the value of that checkbox into a list then pass it from view to controller?




Aucun commentaire:

Enregistrer un commentaire