首页 > 解决方案 > 如何使用 jQuery 更新数据表中的所有多行?

问题描述

我有一个带复选框的薪水数据表,一个选择以填充 chantiers,还有一个按钮 updateAll,当我检查薪水行并在选择中选择一个 chantier 并单击按钮 updateAll 时,我想要按值修改薪水检查在选择中选择。注意:不改变 select 的值,即<option value="1">azilal</option> not <option value="azilal">azilal</option> 在我的例子中不改变 azilal 2 但我想通过 tizgui 改变 azilal,这只是一个例子

数据表

  <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
              <select class="form-control" id="chantier">
                      <option selected disabled>Select Location</option>
                      <option value="1">azilal</option>
                      <option value="2">asfalou</option>
                      <option value="3">tizgui</option>
              </select>
           <button class="btn btn-theme update-all" data-url="">Update All</button>
              <table id="example" class="table table-striped table-bordered" style="width:100%">
                 <thead>
                  <tr>
                    <th><input type="checkbox" id="check_all"></th>
                    <th>nom prenom</th>
                    <th>cin</th>
                    <th>matricule</th>
                    <th>chantier</th>
                  </tr>
                 </thead>
                <tbody>
                  <tr id="1">
                    <td><input type="checkbox" class="checkbox" name="customer_id[]" value="1" /></td>
                    <td>MARZOUK NAJIB</td>
                    <td>Pa130191</td>
                    <td>2925019599</td>
                    <td value="1">azilal</td>
                  </tr>
                   <tr id="2">
                    <td><input type="checkbox" class="checkbox" name="customer_id[]" value="2" /></td>
                    <td>achraf bourki</td>
                    <td>pa5000</td>
                    <td>202020</td>
                    <td value="2">tizgui</td>
                  </tr>
                </tbody>
</table>

jQuery

   <script type="text/javascript">
    $(document).ready(function () {
        $('#check_all').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".checkbox").prop('checked', true);  
         } else {  
            $(".checkbox").prop('checked',false);  
         }  
        });
         $('.checkbox').on('click',function(){
            if($('.checkbox:checked').length == $('.checkbox').length){
                $('#check_all').prop('checked',true);
            }else{
                $('#check_all').prop('checked',false);
            }
         });
        $('.update-all').on('click', function(e) {

          if(confirm("Are you sure you want to update this?"))
  {
   var id = [];
   var chantier;
   $(':checkbox:checked').each(function(i){
    id[i] = $(this).val();
   });
   if(id.length === 0){
    alert("Please Select atleast one checkbox");
   }else{
    let chantier = $('#chantier').val();
      for(var i=0; i<id.length; i++)
      {
        $('tr#'+id[i]).find('td:last-child').html(chantier);
      }  
   }}
  else{
   return false;
  }
    });
    });
</script>
  <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>

标签: jquery

解决方案


在 javascript 中,您正在选择#chantier的选定选项的值,而不是它的文本。
您需要做的就是将这一行更改如下:

旧线:

let chantier = $('#chantier').val();

替换为以下内容:

let chantier = $('#chantier option:selected').html();

推荐阅读