2012-12-06 33 views
2

我有一個DataTable的列之一是複選框。我可以選擇多個複選框。 現在點擊按鈕我想獲得選定複選框的行ID。一旦ajax操作完成 點擊按鈕,我想再次選擇複選框上面的行ID。基本上 如何獲取選中的行ID並再次選中複選框?獲取jQuery數據表中選中複選框的行標識符?

+0

能否請您發佈一些代碼? - http://www.whathaveyoutried.com/ –

+0

它與數據庫無關。我認爲Java腳本就夠了。 –

+0

對不起,這是錯字。更正爲數據表 –

回答

4

假設每個複選框都具有RowId作爲其值屬性。

獲得所有被選中的複選框:

var selectedIds = []; 

$(":checked").each(function() { 
    selectedIds.push($(this).val()); 
}); 

重新檢查複選框:

$.each(selectedIds, function(index, id) { 
    $(":checkbox[value='" + id + "']").prop("checked", true); 
}); 
1

如果我理解正確你的問題,這是你在找什麼,

jQuery代碼

$(document).ready(function() { 
    $('#btn').click(function(){ 
     var dataArr = []; 
     $('input:checked').each(function(){ 
     alert($(this).closest('tr[id]').attr('id'));// just to see the rowid's 
      dataArr.push($(this).closest('tr[id]').attr('id')); // insert rowid's to array 
     }); 
     // send data to back-end via ajax 
     $.ajax({ 
       type : "POST", 
       url : 'server.php', 
       data : "content="+dataArr, 
       success: function(data) { 
        alert(data);// alert the data from the server 
       }, 
       error : function() { 
       } 
     }); 
    }); 
}); 

HTML代碼

<table border="2" cellspacing="2" cellpadding="2"> 
    <tr id="row1"> 
    <td><input id="checkbox1" name="checkbox1" type="checkbox" value="1" /></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr id="row2"> 
    <td><input id="checkbox2" name="checkbox2" type="checkbox" value="2" /></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr id="row3"> 
    <td><input id="checkbox3" name="checkbox3" type="checkbox" value="3" /></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
</table> 
<input id="btn" name="btn" type="button" value="CLICK" /> 
相關問題