2017-05-17 46 views
1

我試圖使用「取消選擇全部」按鈕從本地存儲刪除特定的複選框組。在小提琴中我有兩組複選框。我希望每個小組獨立於另一個工作。因此,如果我選擇說,每個組中的一個國家(即現在在本地存儲中有2個ID),那麼如果單擊一個組的「取消選擇」圖標,我希望另一個組的國家駐留在本地存儲中。此刻,一切都被消滅了。迄今爲止所有努力都失敗了任何想法,我如何才能得到這個工作?小提琴:http://jsfiddle.net/s947vgoh/使用「取消選擇全部」按鈕從本地存儲刪除複選框

$(function() { 
 

 
    // Saving checkboxes to local-storage 
 
    var $containers = $('#CountryListBoxID_prodn, #CountryListBoxID_SECOND'); 
 

 
    $containers.on("change", "input", function() { 
 
    var $checkboxes = $(':checkbox:checked'); 
 
    var selected = $(':checkbox:checked').map(function() { 
 
     return this.id; 
 
    }).get(); 
 
    selected = '#' + selected.join(',#'); 
 
    localStorage.setItem('selected_checkboxes', selected); 
 
    console.log(localStorage.getItem('selected_checkboxes')); 
 
    }); 
 
}); 
 

 
// -------------- 
 
// TOP set of checkboxes 
 

 
$("#CountrySelectAll_ID_prodn").on('click', function() { 
 
    $('#CountryListBoxID_prodn').find("input:checkbox").prop('checked', true); 
 
    var $containers = $('#CountryListBoxID_prodn'); 
 
    localStorage.setItem('selected_checkboxes', '#' + $containers.find("input:checked").map(function() { 
 
    return this.id; 
 
    }).get().join(',#')) 
 
    console.log(localStorage.getItem('selected_checkboxes')); 
 
}); 
 

 
$("#CountrySelectNone_ID_prodn").on('click', function() { 
 
    $('#CountryListBoxID_prodn').find("input:checkbox").prop('checked', false); 
 
    localStorage.removeItem('#CountryListBoxID_prodn'); 
 
    console.log(localStorage.getItem('selected_checkboxes')); 
 
}); 
 

 

 
// -------------- 
 
// LOWER set of checkboxes 
 

 
$("#CountryAll_ID_SECOND").on('click', function() { 
 
    $('#CountryListBoxID_SECOND').find("input:checkbox").prop('checked', true); 
 
    var $containers = $('#CountryListBoxID_SECOND'); 
 
    localStorage.setItem('selected_checkboxes', '#' + $containers.find("input:checked").map(function() { 
 
    return this.id; 
 
    }).get().join(',#')) 
 
    console.log(localStorage.getItem('selected_checkboxes')); 
 
}); 
 

 
$("#CountryNone_ID_SECOND").on('click', function() { 
 
    $('#CountryListBoxID_SECOND').find("input:checkbox").prop('checked', false); 
 
    localStorage.removeItem('#CountryListBoxID_SECOND'); 
 
    console.log(localStorage.getItem('selected_checkboxes')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css" /> 
 
<button type='submit' id="SelectALLCountryID_prodn"> 
 
      <i id="CountrySelectAll_ID_prodn" class="fa fa-check-square-o fa-lg" title="Select All"></i> 
 
</button> 
 
<button type='submit' id="SelectNONECountryID_prodn"> 
 
      <i id="CountrySelectNone_ID_prodn" class="fa fa-square-o fa-lg" title="Unselect All"></i> 
 
</button> 
 
<hr> 
 

 
<div id="CountryListBoxID_prodn"> 
 
    <label class="myEuropeCountries"><input type="checkbox" id="UN40" value="Austria" />Austria</label> 
 
    <label class="myEuropeCountries"><input type="checkbox" id="UN251" value="France" />France</label> 
 
    <label class="myEuropeCountries"><input type="checkbox" id="UN276" value="Germany" />Germany</label> 
 
</div> 
 

 
<br> 
 
<br> 
 
<br> 
 

 
<button type='submit' id="ALLCountryID_SECOND"> 
 
      <i id="CountryAll_ID_SECOND" class="fa fa-check-square-o fa-lg" title="Select All"></i> 
 
</button> 
 
<button type='submit' id="NONECountryID_SECOND"> 
 
      <i id="CountryNone_ID_SECOND" class="fa fa-square-o fa-lg" title="Unselect All"></i> 
 
</button> 
 
<hr> 
 

 
<div id="CountryListBoxID_SECOND"> 
 
    <label class="myEuropeCountries"><input type="checkbox" id="G80" value="Spain" />Spain</label> 
 
    <label class="myEuropeCountries"><input type="checkbox" id="G500" value="Italy" />Italy</label> 
 
    <label class="myEuropeCountries"><input type="checkbox" id="G300" value="UK" />UK</label> 
 
</div>

+0

我看你設置'selected_checkboxes'但別的東西刪除。 – mplungjan

+0

我想我的所有實驗都失敗了!但即使我刪除了selected_checkboxes,它仍然會清除所有內容(即包括來自其他組的任何選項) – Silverburch

+0

請修正setItem和getItem以訪問您需要的實際項目。您正在設置一個字符串並獲取一個字符串。當您刪除項目時,您可以通過給定的鍵刪除字符串。 – mplungjan

回答

1

這裏是點擊一個解決方案的所有/全部刪除,只需點擊一下 - 該代碼還設置了檢查箱子的onload從localStorage的

http://jsfiddle.net/mplungjan/dso6jsnn/

由於安全設置,localStorage不在SO上工作

var chks = { 
 
    "CountryListBoxID_prodn": [], 
 
    "CountryListBoxID_SECOND": [] 
 
} 
 

 
function saveSet(id, checks) { 
 
    chks[id] = checks; 
 
    localStorage.setItem("selected_checkboxes", JSON.stringify(chks)); 
 
    console.log(id,localStorage.getItem("selected_checkboxes")); 
 
} 
 

 
$(function() { 
 

 
    var chks = localStorage.getItem("selected_checkboxes"); 
 
    if (chks) { 
 
    chks = JSON.parse(chks); 
 
    $.each(chks,function(key,val) { 
 
     $.each(val,function(_,id) { 
 
     $("#"+id).prop("checked",true); 
 
     }); 
 
    }); 
 
    } 
 

 
    $(".sel, .remove").on("click", function() { 
 
    var sel = $(this).is(".sel"), 
 
     id = $(this).data("id"), 
 
     $checks = $("#" + id).find(":checkbox"); 
 
    $checks.prop("checked", sel); 
 
    saveSet(id, sel ? $checks.map(function() { 
 
     return this.id 
 
    }).get() : []); // save all or none 
 
    }); 
 
    $(":checkbox").on("click", function() { 
 
    var $container = $(this).closest("div"); 
 
    saveSet($container.attr("id"), $container.find(":checkbox:checked").map(function() { 
 
     return this.id 
 
    }).get()); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css" /> 
 

 
<button type='button' class="sel" data-id="CountryListBoxID_prodn"> 
 
    <i class="fa fa-check-square-o fa-lg" title="Select All"></i> 
 
</button> 
 
<button type='button' class="remove" data-id="CountryListBoxID_prodn"> 
 
    <i class="fa fa-square-o fa-lg" title="Unselect All"></i> 
 
</button> 
 
<hr> 
 

 
<div id="CountryListBoxID_prodn"> 
 
    <label class="myEuropeCountries"> 
 
    <input type="checkbox" id="UN40" value="Austria" />Austria</label> 
 
    <label class="myEuropeCountries"> 
 
    <input type="checkbox" id="UN251" value="France" />France</label> 
 
    <label class="myEuropeCountries"> 
 
    <input type="checkbox" id="UN276" value="Germany" />Germany</label> 
 
</div> 
 

 
<br> 
 
<br> 
 
<br> 
 
<button type='button' class="sel" data-id="CountryListBoxID_SECOND"> 
 
    <i class="fa fa-check-square-o fa-lg" title="Select All"></i> 
 
</button> 
 
<button type='button' class="remove" data-id="CountryListBoxID_SECOND"> 
 
    <i class="fa fa-square-o fa-lg" title="Unselect All"></i> 
 
</button> 
 
<hr> 
 
<div id="CountryListBoxID_SECOND"> 
 
    <label class="myEuropeCountries"> 
 
    <input type="checkbox" id="G80" value="Spain" />Spain</label> 
 
    <label class="myEuropeCountries"> 
 
    <input type="checkbox" id="G500" value="Italy" />Italy</label> 
 
    <label class="myEuropeCountries"> 
 
    <input type="checkbox" id="G300" value="UK" />UK</label> 
 
</div>

+0

您的時間和幫助非常感謝,但我在這裏深入瞭解我的深度。我試過了,但我真的不知道如何使用您發送的代碼。畢竟,這可能不是一個簡單的問題。 – Silverburch

+0

你說得對。代碼的第一部分允許用戶選擇任何複選框,並存儲該標識。第二部分涉及「全選/無」。兩者都需要工作,但因爲代碼的第一部分已經工作,所以最好將它從小提琴中刪除,並專注於「全選/無」,這是行不通的。 – Silverburch

+0

非常感謝小提琴,使我的代碼效率提高了10倍。我可能會錯誤地閱讀控制檯(我習慣了只看到id),但是看起來當兩個組都選中時(即,當6個id在本地存儲器中)時,如果我嘗試「取消選擇」其中一個組,似乎消滅了一切,而不是僅僅來自我不選擇的組中的3個ID。或者是我糟糕地閱讀控制檯? – Silverburch

相關問題