【问题标题】:Select one checkbox row only in datatables仅在数据表中选择一个复选框行
【发布时间】:2013-08-19 20:21:40
【问题描述】:

我希望在 Datatables 中一次只能选择 1 个复选框。

在下面的示例中,可以选择多行。我怎样才能一次只允许选择一行?

http://datatables.net/examples/api/form.html

【问题讨论】:

标签: javascript jquery datatables


【解决方案1】:

您可以使用单选按钮而不是复选框。

例子:

<input type="radio" name="fieldName" value="check1" />
<input type="radio" name="fieldName" value="check2" />
<input type="radio" name="fieldName" value="check3" />
<input type="radio" name="fieldName" value="check4" />

“name”属性是将单选按钮组合在一起,“value”属性用于区分它们。

【讨论】:

  • 你能分享一个例子吗?
【解决方案2】:

How to select first row of the first table in an html page using jQuery? 选择给定表中的一行。 你可以使用jQuery来实现它。

【讨论】:

    【解决方案3】:

    您应该使用单选按钮,

    <form action="path/to/form_submit_script.php" method="get">
      <input type="radio" name="red" value="Red" /> Red<br />
      <input type="radio" name="blue" value="Blue" /> Blue<br />
      <input type="radio" name="green" value="Green" /> Green<br />
      <input type="submit" value="Submit">
    </form>
    

    这是工作示例jsfiddle example

    【讨论】:

      【解决方案4】:
      var _MainDataTable = $('#dataTable').DataTable();
      ...
      $("#dataTable").on('click', 'tr', function () {
          if (_MainDataTable.$(".selected").length > 1) {
              _MainDataTable.$(".selected").removeClass('selected');
              $(this).addClass("selected");
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-09
        • 2020-11-20
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 2015-08-16
        相关资源
        最近更新 更多