2016-07-07 26 views
0

我用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")) 

任何想法?

+0

你有沒有考慮過使用單選按鈕? – ymas

+0

@ymas nop我只是想知道爲什麼它沒有取消選中它自己的行的複選框,它也改變了所有其他行。我知道它是因爲'class',但我應該如何使它與我現有的代碼一起工作?我想我會遇到與上面的代碼相同的問題。 –

+0

它更改其他行,因爲類名稱用於檢查和取消選中複選框,並且類不是唯一標識符。而不是使用類名稱使用Id。 –

回答

3

就你而言,它.false和.true將取消全部選中。你只需要檢查最近的元素。

請替換代碼$('。true')。attr('checked',false);這個代碼$(this).closest(「td」).find('。true')。attr('checked',false);

並對$('。false')做同樣的處理。attr('checked',false);這個$(​​this).closest(「td」).find('。false')。attr('checked',false);

+0

非常感謝。這工作就像一個魅力:) –