简体   繁体   English

仅在数据表中选择一个复选框行

[英]Select one checkbox row only in datatables

I want to be able to select only 1 checkbox at a time in Datatables. 我希望一次只能在Datatables中选中1个复选框。

In the example below, multiple rows can be selected. 在下面的示例中,可以选择多行。 How can I go about only allowing one row to be selected at a time? 如何只允许一次选择一行?

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

You could use Radio buttons instead of checkboxes. 您可以使用单选按钮代替复选框。

Example: 例:

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

The "name" attribute is what groups the radio buttons together, the "value" attribute is what will differentiate them. “名称”属性是将单选按钮组合在一起的功能,“值”属性是将单选按钮区分开的功能。

How to select first row of the first table in an html page using jQuery? 如何使用jQuery在html页面中选择第一个表格的第一行? Selects one row in a given table. 在给定表中选择一行。 You could use jQuery to achieve it. 您可以使用jQuery实现它。

You should use the radio button, 您应该使用单选按钮,

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

Here is the working example jsfiddle example 这是工作示例jsfiddle示例

var _MainDataTable = $('#dataTable').DataTable();
...
$("#dataTable").on('click', 'tr', function () {
    if (_MainDataTable.$(".selected").length > 1) {
        _MainDataTable.$(".selected").removeClass('selected');
        $(this).addClass("selected");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM