我用PHP foreach(在codeigniter中)創建的表格有一個非常簡單的問題。與PHP foreach中的表格中的複選框
表格中的一列有2個複選框。一個true
和一個false
所以當我創建表有一些名稱和信息,然後2複選框。每當用戶點擊true
我創建一個AJAX POST調用到我的數據庫來更改列的狀態。但是,當用戶點擊true
,然後他點擊false
我想要取消選中true
複選框。
下面是代碼
<table class="responstable">
<tr>
<th>ID</th>
<th><span>Name</span></th>
<th>number1</th>
<th>number2</th>
</tr>
<?php
foreach($students as $column => $data) { ?>
<tr>
<td><?php echo $data[0]->Userproperties_UserAM; ?></td>
<td><?php echo $data[0]->Userproperties_UserFullName; ?></td>
<td><?php echo $data[0]->UserAbsence; ?></td>
<td> <input class="true" id="<?php echo $data[0]->Userproperties_UserId; ?>" type='checkbox' value="<?php echo $data[0]->Userproperties_UserAM; ?>" />
<label for='true'>
<span></span>
True
</label>
<input class="false" id="<?php echo $data[0]->Userproperties_UserId; ?>" type='checkbox' value="<?php echo $data[0]->Userproperties_UserAM; ?>" />
<label for='false'>
<span></span>
False
</label></td>
</tr>
<?php } ?>
</table>
而Ajax代碼
$(document).ready(function() {
$('.true').change(function() {
if($(this).is(":checked")) {
$('.false').attr('checked', false);
var moduleid = <?php echo $moduleid; ?>;
var teacherid = <?php echo $teacherid; ?>;
var studentid = $(this).attr('id');
var weeknum = $("#dropi").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/TeacherParousies/send_parousia",
data: {
moduleid: moduleid,
studentid: studentid,
teacherid: teacherid,
parousia: 1,
weeknum: weeknum
},
success: function(res) {
},
error: function(err) {
}
});
}
});
$('.false').change(function() {
if($(this).is(":checked")) {
$('.true').attr('checked', false);
var moduleid = <?php echo $moduleid; ?>;
var teacherid = <?php echo $teacherid; ?>;
var studentid = $(this).attr('id');
var weeknum = $("#dropi").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/TeacherParousies/send_parousia",
data: {
moduleid: moduleid,
studentid: studentid,
teacherid: teacherid,
parousia: 0,
weeknum: weeknum
},
success: function(res) {
},
error: function(err) {
}
});
}
});
});
這裏的問題是,當我檢查表的複選框,讓說false
的第2行中,有一個變化如果它被選中,則爲true
的第一行。
雖然我已經指出
if($(this).is(":checked"))
任何想法?
你有沒有考慮過使用單選按鈕? – ymas
@ymas nop我只是想知道爲什麼它沒有取消選中它自己的行的複選框,它也改變了所有其他行。我知道它是因爲'class',但我應該如何使它與我現有的代碼一起工作?我想我會遇到與上面的代碼相同的問題。 –
它更改其他行,因爲類名稱用於檢查和取消選中複選框,並且類不是唯一標識符。而不是使用類名稱使用Id。 –