2012-02-25 77 views
0

我一直在做這件事幾天,我似乎無法找到正確的方法來做到這一點。當用戶取消選中複選框時,我需要提交該複選框的ID,然後將其刪除到表中。不是選擇器(不知道我是否正確地做)

// JS

<script> 
$(document).ready(function(){ 
    $('#list-tbl tr').each(function(){ 
     var $this = $(this); 
     var ID = $(this).attr("id").replace("tr",""); 
     var user_id = $('#user_id').val(); 
     var read = $('#read'+ID).val(); 
     var add = $('#add'+ID).val(); 
     var approve = $('#approve'+ID).val(); 

     $('input[type=checkbox]', $this).change(function(){ 
      if($('.read'+ID, $this).is(':checked')){ 
       $('input[type=checkbox]', $this).prop('disabled',true); 
       $.ajax({ 
        type: "POST", 
        url: "<?= base_url('admin/update/read')?>", 
        data: "read="+ read + "&user_id=" + user_id, 
        success: function(){ 
         $('form#submit').hide(); 
          alert("Permission added"); 
         $('div.success').fadeIn(); 
        } 
       }); 
       $(this).prop('disabled',false); 
      } else if($('.add'+ID, $this).is(':unchecked')) { 
       $('.read', $this).prop('disabled', true); 
       $.ajax({ 
        alert("FU"); 
        } 
       }); 
       $(this).prop('disabled', false); 
      } else if($('.add'+ID, $this).is(':checked')) { 
       $('.read', $this).prop('disabled', true); 
       $.ajax({ 
        type: "POST", 
        url: "<?= base_url('admin/update/add')?>", 
        data: "add="+ add + "&user_id=" + user_id, 
        success: function(){ 
         $('form#submit').hide(); 
          alert("Permission add added"); 
         $('div.success').fadeIn(); 
        } 
       }); 
       $(this).prop('disabled', false); 
      } else if($('.approve'+ID, $this).is(':checked')) { 
       $('.read', $this).prop('disabled', true); 
       $.ajax({ 
        type: "POST", 
        url: "<?= base_url('admin/update/approve')?>", 
        data: "approve="+ approve + "&user_id=" + user_id, 
        success: function(){ 
         $('form#submit').hide(); 
          alert("Permission approve added"); 
         $('div.success').fadeIn(); 
        } 
       }); 
       $(this).prop('disabled', false); 
      } 
      else{ 
       $('input[type=checkbox]', $this).prop('disabled',false); 
       $(this).prop('disabled',false); 
      } 
     }); 
    }); 
}); 

而且還我想初始化複選框應該是什麼。規則是。

當用戶有讀取權限(添加&批准應該被禁用)。 當用戶添加/批准時(只應禁用讀取)。

在我的jQuery腳本它已經工作,但如果它的重裝觸發它不..

HTML:

<table width="100%" id="list-tbl"><form id="permissions2"><tr id="tr2"><td>Sia Olsen</td><td align="right"><input type="hidden" name="user_id" id="user_id" value="2" />Read&nbsp;&nbsp;&nbsp;<input type="checkbox" name="read2" id="read2" class="read2" checked />Add&nbsp;&nbsp;&nbsp;<input type="checkbox" name="add2" id="add2" class="add2" checked />Approve&nbsp;&nbsp;&nbsp;<input type="checkbox" name="approve2" id="approve2" class="approve2" checked /></td></tr></form><form id="permissions3"><tr id="tr3"><td>Mary Lee</td><td align="right"><input type="hidden" name="user_id" id="user_id" value="3" />Read&nbsp;&nbsp;&nbsp;<input type="checkbox" name="read3" id="read3" class="read3" checked />Add&nbsp;&nbsp;&nbsp;<input type="checkbox" name="add3" id="add3" class="add3" />Approve&nbsp;&nbsp;&nbsp;<input type="checkbox" name="approve3" id="approve3" class="approve3" /></td></tr></form></table> 
</div></div> 
+0

目前尚不清楚複選框的用途。一方面,它們似乎是切換狀態的控制,但另一方面,它們似乎是服務器端狀態的指示器。 – 2012-02-25 03:33:46

+0

YE。事實上我遇到的問題是當我使用.not(:checked)選擇器時。它始終指向未檢查... – 2012-02-25 04:29:37

回答

1

你必須交替「檢查」屬性,基於用戶數據。基本上,它應該是這樣的:

<input type="checkbox" name="read2" <?php if ($is_readable) { echo 'checked'; } ?> > 
相關問題