gpt4 book ai didi

javascript - 除表头之外的表不起作用 html

转载 作者:行者123 更新时间:2023-12-04 08:43:35 26 4
gpt4 key购买 nike

单击标题中的复选框时,我一直试图选择表格中的所有复选框,但问题是当我尝试提交所有选定的列表时,它包含复选框标题。我想要的是只有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com