2015-06-25 62 views
0

檢查了所有/取消選中所有,但限制一行檢查

<thead> 
     <tr> 
      <th>Num</th> 
      <th>name</th> 
      <th>date</th> 
      <th><input type="checkbox" name="m_check" id="m_check" /></th> 
     </tr> 
    </thead> 
    <tbody> 
     <!-- i have loop for get data --> 
     <tr> 
      <td>$Num</td> 
      <td>$name</td> 
      <td>$date</td> 
      <td><input type="checkbox" name="e_check[]" value="<?php echo $companys->data[$i]['com_id'] ?>" class ="e_check" id="e_check_<?php echo $companys->data[$i]['com_id'] ?>" /></td> 
     </tr> 
     <!-- end loop for get data --> 
    </tbody> 

這是我的腳本

$('#m_check').change('change',function() { 

     if ($(this).is(':checked')) { 
      $('input[name="e_check[]"]:checkbox').attr('checked', true); 
      $('#ib_email').removeAttr("disabled"); 
     } else { 
      $('input[name="e_check[]"]:checkbox').attr('checked', false); 
      $('#ib_email').attr("disabled", "disabled"); 
     } 
}); 

我的問題是我需要的時候用戶就m_check檢查它的e_check但檢查檢查元素只有10個,如果我的e_check還有10多個。

請幫我糾正我的js

我也看到this,但我仍然不能自定義我的代碼:(

**確認我的代碼是沒有錯的用戶檢查了所有,這是檢查牙齒列所有,未經檢查和它是否被選中的所有元素行,但是當用戶檢查所有的按鈕它檢查元素行的限制,10排頂等仍然沒有檢查

+2

代替'$( '#m_check')。改變( '變',函數(){'和'$( '#m_check')。在( '變', '#m_check',函數( ){' – Umair

+0

http://jsfiddle.net/tm6SH/56/ –

+0

@Learner謝謝:) – Songs

回答

0

我需要你應該做這樣

$(document).on('change', '#m_check', function() { 

$('#m_check').click(function() { 
      if ($(this).is(':checked')) { 
       $('input[type=checkbox].e_check').map(function (_, el) { 
        $(el).prop('checked', true); 
       }); 
      } else { 
       $('input[type=checkbox].e_check').map(function (_, el) { 
        $(el).prop('checked', false); 
       }); 
      } 


     }); 
}); 

$('#m_check').change(function() { 


}); 

編輯:

現在,當你點擊<input type="checkbox" name="m_check" id="m_check" />您記錄所有將被檢查。

+0

這不是我的代碼錯了,但我的代碼是當用戶檢查所有,它檢查所有元素行,但我想檢查限制行 – Songs

+0

@歌曲請再看一遍...我再次更新..當你點擊檢查所有...你所有的記錄將被檢查...當你將取消檢查所有...所有您的記錄將被取消選中 – Umair

0

在jQuery中有幾個錯誤。應該使用道具來代替被檢查道具的屬性。當name =「e_check []」已經是一個唯一標識符時,':checkbox'也是不必要的。最後,檢查底部的代碼片段以獲取數字。有了它,您可以設置檢查行爲以反映抓取的數字。

$('#m_check').change('change', function() { 

    if ($(this).is(':checked')) { 
     $('[name="e_check[]"]').prop('checked', true); 
     $('#ib_email').removeAttr("disabled"); 
    } else { 
     $('[name="e_check[]"]').prop('checked', false); 
     $('#ib_email').attr("disabled", "disabled"); 
    } 

    var $num = $(this).closest('table').find('td:first'); 
    $num = parseInt($num); 
}); 
+0

請檢查我的更新 – Songs

0

你可以這樣做。

我假設有類tablecheckbox attachewd到TBODY到 唯一標識的複選框。

$('#m_check').on('change',function() { 
    if ($(this).is(':checked')) { 
     if($(".tablecheckbox tr input[type='checkbox']").length > 10){ 
      $(".tablecheckbox tr input[type='checkbox']").slice(0,10).prop('checked',true) 
     }else{ 
      $(".tablecheckbox tr input[type='checkbox']").prop('checked',true) 
     } 

     $('#ib_email').removeAttr("disabled"); 
    } else { 
     $(".tablecheckbox tr input[type='checkbox']").slice(0,10).prop('checked',false) 
     $('#ib_email').attr("disabled", "disabled"); 
    } 
    }); 
+0

這是工作,謝謝 – Songs

相關問題