jquery - 如何使用 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>
解决方案
在 javascript 中,您正在选择#chantier的选定选项的值,而不是它的文本。
您需要做的就是将这一行更改如下:
旧线:
let chantier = $('#chantier').val();
替换为以下内容:
let chantier = $('#chantier option:selected').html();
推荐阅读
- mongodb - 在 mongoDB 中添加分片时连接被拒绝
- sql - 根据其他列对列进行排序
- php - Laravel 尝试访问服务提供者中的 Sentinel 用户以与所有视图共享它给 null
- javascript - 顶级函数中的“this”
- javascript - 如何使用 JavaScript 或 jQuery 防止多次点击?
- java - 为什么我的 JLabel 没有显示?
- javascript - 在 ReactJS 中使用 Real DOM 元素修改 VirtualDOM
- docker - 将 KafkaConnect 添加到现有的 Ambari 服务
- javascript - Lodash 将键的值映射到另一个键
- javascript - 从另一个网页加载命名元素