checkbox的全選、反選、多選等操作(js)

來源:互聯網
上載者:User

標籤:checked   js   多選   反選   單選   

以下控制項的name屬性為DEPT_ID 
(案例中預設選中的checkbox,全選或者反選,對該控制項無影響)
//1.全選

function selectAllRight(){

$("input[name=‘DEPT_ID‘").each(function() {  

    $(this).prop("checked", true); 

 }); 

}

//2.反選
function reverseSelectRight(){
    $("input[name=‘DEPT_ID‘").each(function() {  
    //若當前選項沒被選中,則設定為選中
    if(false == $(this).is(‘:checked‘)){
        $(this).prop("checked", true);
    }else{
    //若當前選項選中,但是不是為disabled的話,則設定為取消選中
    if(false == $(this).prop("disabled")){
        $(this).prop("checked", false);
    }}}); }

//3.進行選擇控制,若且唯若只允許選擇一個。
$("input[name=‘DEPT_ID‘]").click(function() {
$("input[name=‘DEPT_ID‘").each(function() {              
//若當前當前不為disable,並且被選中,則取消選中            
if(true ==$(this).is(‘:checked‘)){            
if(true == $(this).prop("disabled")){
    $(this).prop("checked", true);
}else{
    $(this).prop("checked", false);
}}});
//當前設定選中狀態
    $(this).prop("checked", true);
});

//4.重設
function abandonSelectRight(){
    //擷取所有被選中的部門
        $("input[name=‘DEPT_ID‘").each(function() {  
    //若當前選項沒被選中,則設定為選中
    if(true == $(this).is(‘:checked‘)){
    //若當前選項選中,但是不是為disabled的話,則設定為取消選中
    if(false == $(this).prop("disabled")){
        $(this).prop("checked", false);
    }}
    });
}


控制項:
<label><input name="DEPT_ID" type="checkbox" value="100100" />控制項1 </label>

<label><input name="DEPT_ID" type="checkbox" value="100100" />控制項2 </label>

<label><input name="DEPT_ID" type="checkbox" value="100100" />控制項3 </label>

本文出自 “雷氏出品” 部落格,請務必保留此出處http://leoky.blog.51cto.com/752840/1871889

checkbox的全選、反選、多選等操作(js)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.