我正在尝试使用切换按钮更新数据库中的值 这是我的剧本
$(document).ready(function () {
$('input.status').on('change', function () {
var decision = $(this).val();
var id = $('td.myvalue').html();
alert(decision);
alert(id);
$.ajax({
type: "POST",
url: "/CodeIgniter/users/Users/update",
data: {decision: decision, id: id},
success: function (msg) {
$('#autosavenotify').text(msg);
}
})
});
});
数据表
<table class="table table-striped table-hover DataTables "id="DataTables" cellspacing="0" width="100%">
<thead>
<tr>
<th>Icon</th>
<th>Name</th>
<th>Group Users</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i = 0;
foreach ($getTableGroup as $value) { ?>
<tr>
<td class="myvalue">
<?= $value->id ?>
</td>
<td>
<?= $value->group_name ?>
</td>
<td>
<input class="tagsinput form-control" type="text" value="sam"/>
</td>
<input class="myid" type="hidden" value="<?= $value->id ?>"/>
<td >
<?php if ($value->group_status == 'on'): ?>
<div class="switch">
<div class="onoffswitch">
<input type="checkbox" checked class="onoffswitch-checkbox status" id="example<?= $i ?>" name="group-status" >
<label class="onoffswitch-label" for="example<?= $i ?>">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
<?php elseif ($value->group_status == 'off'): ?>
<div class="switch">
<div class="onoffswitch">
<input type="checkbox" checked class="onoffswitch-checkbox status" id="example<?= $i ?>" name="group-status">
<label class="onoffswitch-label" for="example<?= $i ?>">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
<?php endif; ?>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
我的代码运行正常,但问题出现在脚本部分,当我点击切换按钮时,每次id在警告框中出现时id都不会增加,但是在datatable中它显示来自数据库的Id编号
答案 0 :(得分:0)
您总是先获取td
值$('td.myvalue')
这是错误的,您需要获取当前更改的tr td
值
var id = $('td.myvalue').html(); //it will always return the first td only .
更改为
var id = $(this).parent('div').parent('div').parent('td').parent('tr').find('td.myvalue').html();
答案 1 :(得分:0)
您可以使用以下代码来减少更多父级。
var id = $(this).closest('tr').find('td.myvalue').text();