【问题标题】:Getting the row ids for the checked checkboxes in jquery dataTable? [closed]获取jquery dataTable中选中复选框的行ID? [关闭]
【发布时间】:2012-12-06 11:12:13
【问题描述】:

我有一个 DataTable,其中一列是复选框。我可以选择多个复选框。 现在单击按钮,我想获取选定复选框的行 ID。一旦ajax操作完成 单击按钮时,我想再次选择上述行 ID 的复选框。基本上 如何获取选中的行 ID,然后再次选中复选框?

【问题讨论】:

  • 你能发一些代码吗? - whathaveyoutried.com
  • 与数据库无关。我认为Java脚本就足够了。
  • 抱歉打错了。更正为数据表

标签: javascript jquery html datatable


【解决方案1】:

假设每个复选框都有 RowId 作为他的值属性。

获取所有选中的复选框:

var selectedIds = [];

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

重新勾选复选框:

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

【讨论】:

    【解决方案2】:

    如果我正确理解了你的问题,这就是你要找的,

    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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2021-04-27
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多