2013-07-05 49 views
-2

嗨我試圖添加到選中的複選框的值,所以保持選定的值,但添加其他選定的值。但是該屬性不會添加選定的值。這裏是我的嘗試(PS我怎麼會反過來刪除選定的值,並保持選定的現有價值?)感謝通過jquery添加到複選框中的選中值

$('.add_button').click(function() { 

      var values = $('input:checkbox:checked.ssremployeeids').map(function() { 
       return this.value; 
      }).get(); 

     $('.ssremployeeid').attr('selected',values); 


     <div class="grs-multi-select-area" style="height:120px;width:150px;"> 
    <div class="grs-multi-select-box "> 
    <input id="ssrBox" class="ssremployeeid" type="checkbox" name="ssremployeeid[]" 
value="1312"> 
     Amanda Becker 
    </div> 
    <div class="grs-multi-select-box "> // same as above I just collapsed it for viewing purposes 
    <div class="grs-multi-select-box "> 
    <div class="grs-multi-select-box "> 
    </div> //closes main div 

    }); 
+0

我很困惑。需要添加什麼以及需要刪除什麼。太多的添加/刪除在你的問題上繼續。:D – Learner

+0

@學習者對於混淆抱歉再次編輯了問題。謝謝 – Squirtle

+0

你爲什麼試圖獲得選定項目的值,然後將它們分配回去? – Learner

回答

1

我仍然不知道我的理解是否正確的問題,而是基於一個我的理解這裏的東西你可以嘗試:

樣品小提琴:http://jsfiddle.net/HFULf/1/

HTML

<div id="div1"> 
     <input type="checkbox" name="cb1" value="val1" checked="checked" /> 
     <input type="checkbox" name="cb2" value="val2"/> 
     <input type="checkbox" name="cb3" value="val3"/> 
</div> 
<div id="div2"> 
     <input type="checkbox" name="cb1" value="val1"/> 
     <input type="checkbox" name="cb2" value="val2"/> 
     <input type="checkbox" name="cb3" value="val3"/> 
</div> 

<button id="btn1">Add Selected</button> 
<button id="btn2">Remove Selected</button> 

jQuery的

//add selected values from first set of check-boxes to the second set 
$('#btn1').click(function(e){ 
    $('div#div1 input[type="checkbox"]:checked').each(function(){ 
     $('div#div2 input[type="checkbox"]').eq($(this).index()).prop('checked',true); 
    }); 
}); 

//remove values from second set of check-boxes if selected in first one 
$('#btn2').click(function(e){ 
    $('div#div1 input[type="checkbox"]:checked').each(function(){ 
     $('div#div2 input[type="checkbox"]').eq($(this).index()).prop('checked',false); 
    }); 
}); 

希望,這將幫助你一點,如果不能完全解決你的問題。