-3

I have a Table. In this table, the user can select all. or 3-4 records can be selected. What I need to do is to get the ids of the records he chose, but I couldn't do that.

HTML TABLE

 <table id="kisitListesi" class="table mb-0 table-center">
                        <thead>
                            <tr>
                                <th class="border-bottom text-start py-3"><input  id="kisitselectall" type="checkbox" /> Tümünü Seç</th>
              <th class="border-bottom text-start py-3">Şirket</th>
              <th class="border-bottom text-start py-3">Kullanıcı</th>                                                              <th class="border-bottom text-start py-3">Kısıt</th>
               <th class="border-bottom text-center py-3">Diğer</th>
   </tr>
     </thead>
  <tbody>
  @foreach (var item in AdminStatic.stokKisitiOlanKullanicilar())
   {
         <tr class="shop-list">
        <td class="border-bottom text-start py-3"><input class="kisitsec" data-idd="@item.ID" type="checkbox" /></td>       <td>@AdminStatic.GetKullaniciIsletme(Convert.ToInt32(item.Isletme)).IsletmeAdi</td>
           <td>@item.AdSoyad</td>
   <td>@item.Kisit</td>
     <td class="text-center">
            <span class="badge rounded-pill bg-success">
          <a style="color:white; margin:auto;" href="javascript:void(0)" onclick="KisitDuzenle(@item.ID">
       <i class="uil uil-edit"></i>
            </a>
          </span>
         <span class="badge rounded-pill bg-danger">
      <a style="color:white; margin:auto;" href="javascript:void(0)"onclick="@item.ID">
                 X
        </a>
       </span>

    </td>
      </tr>
             }
      </tbody>
   </table>

JAVASCRİPT

 function seciliDatalar() {
            var result = [];
            $('input:checkbox:checked').each(function (item) {
                result.push($(".kisitsec").attr("data-idd"));
            });
            alert(result)
        }
Don't Panic
  • 13,965
  • 5
  • 32
  • 51

1 Answers1

1

The issue is that $(".kisitsec") always returns all of the elements that have the .kisitsec class and then .attr("data-idd") returns the attribute from the the first matched element.

What you want to do instead is to use the this context within your each loop to get the currently iterated element:

var result = [];
$('input:checkbox:checked').each(function () {
  result.push($(this).attr("data-idd"));
});

An alternative way to achieve the same thing is by using jQuery's .map and .get:

const result = $('input:checkbox:checked').map(function () {
  return $(this).attr("data-idd");
}).get();

Here is an example fiddle.

76484
  • 8,498
  • 3
  • 19
  • 30