如何从动态表中选择一个或另一个或不选择复选框? [英] How to select one or other or no checkbox from dynamic table?
问题描述
我创建了一个动态表.在每一行中,我添加了两个复选框buy
和sell
,每个复选框都有一个.checkboxClass
类.我希望用户能够选择buy
或sell
,但不能同时选择两者.
I have created a dynamic table. In each row, I've added two checkboxes, buy
and sell
, each with a .checkboxClass
class. I want the user to be able to pick either buy
or sell
, but not both.
到目前为止,这是我的代码:
Here's my code so far:
$('input.checkboxClass').on('click', function() {
$('input.checkboxClass').not(this).prop('checked', false);
});
这仅适用于一行.随着添加更多行,它会失败.这是因为checkboxClass
在整个表格中都重复出现,这意味着该脚本仅允许您一次选择一个复选框.
This works for only one row. As more rows are added, it fails. This is because the checkboxClass
is repeated all throughout the table, meaning the script only lets you select one checkbox at a time.
推荐答案
我不敢相信到目前为止还没有人提出这个建议.您的问题是对HTML的普遍误解.这不是不是复选框.您应该使用单选按钮:
I can't believe no one has suggested this so far. Your problem is a general misunderstanding of HTML. This is not what checkboxes are for. You should be using radio buttons:
<div>
<input type="radio" name="radio1" value="buy" id="buy">
<label for="buy">buy</label>
<input type="radio" name="radio1" value="sell" id="sell">
<label for="sell">sell</label>
</div>
这将为您自动实现此功能.不需要脚本.
This will automate this functionality for you. No script required.
这篇关于如何从动态表中选择一个或另一个或不选择复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!