jquery 如何从选中的复选框中获取id以在razor上更新它?

chy5wohz  于 2023-06-22  发布在  jQuery
关注(0)|答案(1)|浏览(158)

我在www.example.com剃刀上工作asp.net。我面临的问题是我无法获得ID组选中复选框为真
更新它的剃刀,所以你能告诉我如何得到id值,已复选框为真
我需要当点击按钮reprintdatabtn,然后检查行复选框选中了真,然后从这些行中提取ID,并将其传递给剃刀更新它
我尝试什么

<button id="reprintdatabtn" style="margin-Left:10px;width:100px;" class="btn btn-primary">Reprint</button>

<table id="example" class="display">
    <thead>
        <tr>
            
            <th><input id="chkHeader" type="checkbox"></th>
            <th>EntredDatetime</th>
            <th>OrderType</th>
            <th>OrderNo</th>
            <th>PrinterName</th>
            <th>BranchCode</th>
            <th>Status</th>
            <th>Id</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var reprint in Model.Reprintslist)
        {
            <tr>

                <td><input id="chkSel" class="myCheckbox" type="checkbox"></td>
                <td>@reprint.LoggedDate</td>
                <td>@reprint.OrderType</td>
                <td>@reprint.OrderNo</td>
                <td>@reprint.PrinterName</td>
                <td>@reprint.BranchCode</td>
                <td>@reprint.Status</td>
                <td id="idval">@reprint.id</td>
        
            </tr>
        }
    </tbody>
</table>

使用jQuery我做如下:

$('#reprintdatabtn').click(function () {
               var dataArr = [];
               $('input:checked').each(function () {

                   dataArr.push($(this).attr('id')); // insert rowid's to array
               });
               console.log(dataArr);

在剃刀页面模型

public async task onpost()
{
How to get id 16810,16811 to update it 
}

我需要的图像如下

预期结果,因为发布的图像必须获得ID
16810和16811

2wnc66cl

2wnc66cl1#

将JavaScript代码更改为:

<script>
        $('#reprintdatabtn').click(function () {
            var dataArr = [];

            // Iterate over checked checkboxes and extract IDs
            $('input[id="chkSel"]:checked').each(function () {
                dataArr.push($(this).closest('tr').find('#idval').text());
            });

            // Send dataArr to the Razor page
            $.ajax({
                url: 'url',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("XSRF-TOKEN",
                        $('input:hidden[name="__RequestVerificationToken"]').val());
                },
                type: 'POST',
                data: { selectedIds: dataArr },
                success: function (response) {
                    // Handle success response
                },
                error: function (xhr, status, error) {
                    // Handle error
                }
            });
        });
    </script>

然后你可以在后台得到id的值

相关问题