javascript - 除表头外的表无法正常工作 html

标签 javascript html jquery ajax

当单击标题中的复选框时,我一直尝试选择表格中的所有复选框,但问题是当我尝试提交所有选定的列表时,它包含复选框标题。我想要的只是 tbody 应该返回值

enter image description here

what I've already tried, I change

  $(':checkbox:checked')each(function(i){
        id[i]=$(this).val()
      })

To this , but not working

$(':checkbox:checked')not('thead tr').each(function(i){
        id[i]=$(this).val()
   })

my table

<button type="submit" id="update_btn" class="btn btn-secondary round btn- 
    min-width mr-1 mb-1" >Update Tag</button>
<table class="table" name="table" id="table">
            <thead>
              <tr >
                <th >No</th>
                <th><input type="checkbox" onClick="toggle(this)" /></th>
                <th>ID Number</th>
                <th >Name</th>
                <th >Barangay</th>
                <th >Sex</th>
                <th >Sector</th>
                <th Amount</th>
                <th >Signature/Thumb Marks</th>
                <th >ID Presented/ Date & Issuance</th>
                <th>Date Received</th>
                <th >Remarks</th>
              </tr>
            </thead>
            <tbody class="report-content">
                {% csrf_token %}
                {% for user_information in benefeciaries %}
              <tr id="{{user_information.id}}">
                <td></td>
                <td><input type="checkbox" name="product_id[]" value=" 
                {{user_information.id}}" 
                id="delete_product"></td>
                <td>{{user_information.covid_id}} </td>
                <td>{{user_information.lastname}}, 
                 {{user_information.firstname}} 
                {{user_information.middle}}</td>
                <td width="5%">{{user_information.barangay}}</td>
                <td></td>
                <td ></td>
                <td>{{user_information.amount|floatformat:2|intcomma}} 
               </td>
                <td></td>
                <td ></td>
                <td ></td>
                <td></td>
              </tr>
              {% endfor %}  
            </tbody>
          </table>

my javascript

  $(document).ready(function(){
  $('#update_btn').click(function(){
    if(confirm("Are you sure ?")){
      var id =[];
      var csrf=$('input[name=csrfmiddlewaretoken]').val();    
      $(':checkbox:checked').each(function(i){ //I think this is the 
       problem
        id[i]=$(this).val()
      })
      console.log(id)
      $.ajax({
          url:"/update/",
          method:"POST",
          data: {
            id,
            'prov':"{{province}}",
            'muni':"{{municipal}}",
            csrfmiddlewaretoken:csrf
          },
          success:function(response){       
            url:"/dashboard/" 
            // Swal.fire({title:"Successfully saved!",text:"Mark As 
          Paid",type:"success",confirmButtonClass:"btn btn- 
          success",buttonsStyling:!1})
          }
        })
      
    }
  })
});

最佳答案

如果您只想选择 tbody 元素中的复选框,请将其包含在选择器中:

$('tbody :checkbox:checked')

此外,您可以使用 map() 而不是 each() 来简化创建选定复选框值数组的逻辑。试试这个:

let id = $('tbody :checkbox:checked').map((i, el) => el.value).get();`

这是完整的示例,没有对逻辑进行一些其他改进:

jQuery($ => {
  $('#update_btn').click(function() {
    if (!confirm("Are you sure ?"))
      return;

    let csrf = $('input[name=csrfmiddlewaretoken]').val();
    let id = $('tbody :checkbox:checked').map((i, cb) => cb.value).get();

    $.ajax({
      url: "/update/",
      method: "POST",
      data: {
        id,
        'prov': "{{province}}",
        'muni': "{{municipal}}",
        csrfmiddlewaretoken: csrf
      },
      success: function(response) {
        url: "/dashboard/"
        /*
        Swal.fire({
          title: "Successfully saved!",
          text: "Mark As Paid",
          type: "success",
          confirmButtonClass: "btn btn-success",
          buttonsStyling: false
        })
        */
      }
    });
  })
});

关于javascript - 除表头外的表无法正常工作 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64443147/

相关文章:

html - z-index 不影响子元素

jquery - 在 jQuery AJAX 调用中返回多个值

javascript - ipython 等效于 node.js 的 javascript/coffeescript?

javascript - 观察传入 HTTP 请求的连接终止 - Node.JS

javascript - 使用字段集的多步表单

HTML5 和 CSS3 的 JavaScript 检查器

javascript - 如何用变量值替换所有带有特定前缀的字符串

java - Eclipse:无法创建 SWT 浏览器小部件

javascript - 我想在脚本找到字符串 "art"时调用该函数 - 怎么做?

javascript - 如何使用 jQuery 在可见和隐藏之间切换(切换、交替)元素?