简体   繁体   中英

Dynamic table with a checkbox per row and only one can be checked (true)

I know similar questions have been asked but no one helped me with my problem.

I am displaying a dynamic table with all result from a sql request and the user has to choose one row by checking a checkbox but ONLY one. The thing is I don't know how to do this with a dynamic table.

Could someone tell me what I did wrong or what could be improved Here is my html and javascript :

 $("input:checkbox").on('click', function() { var $box = $(this); if ($box.is(":checked")) { var group = "input:checkbox[name='" + $box.attr("name") + "']"; $(group).prop("checked", false); $box.prop("checked", true); } else { $box.prop("checked", false); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="table" border="1"> <thead> <tr> <th>Position</th> <th>Quantity</th> <th>Select</th> </tr> </thead> <tbody> <tr ng-repeat="row in msg.payload"> <td ng-repeat="item in row" >{{item}}</td> <td ><input type="checkbox" class="radio" value="1" name="row[1][]" /></td> </tr> </tbody> </table>

只需要通过 type="radio" 更改 type="checkbox"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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