jQuery - 记录复选框点击的顺序
我有一个系统正在跟踪表演艺术中心的志愿者。当他们报名轮班工作时,他们还可以报名一个(或多个)伙伴。如果报名人数过多,也会有等候名单。选择好友的表格看起来像这样:
如果等待名单上只剩下一个位置,我需要如果选择了下一个人的名字,则将其变成红色(因此选择了约翰,但当选中简时,她会变成红色以作为警告)。
我的问题是,用户可能决定同时选中它们,然后取消选中约翰而不是简,这会让简再次变黑。
有没有一种简单的方法可以使用类似的方法来跟踪点击:
$("input:checkbox").change(function(){
$("input:checked").each(function(i){
//object/array manipulation?
});
});
我使用元素的索引可以正常工作,但意识到这是行不通的,因为它会跟踪首先检查复选框的顺序。
即使方向正确,也会令人惊奇。我意识到我没有付出太多来继续下去。
谢谢,
杰森
I have a system that is tracking volunteers for a performing arts center. When they sign up to work a shift they can also sign up a buddy (or buddies). There's also a waiting list if we have too many sign-ups. The form looks like so for selecting buddies:
If the waiting list only has one position left on it I need to turn the next person's name red if they are selected (so John is selected, but when Jane is checked she turns red to serve as a warning).
My problem is that the user might decide to check them both, then uncheck John instead of Jane, which should then make Jane turn black again.
Is there an easy way to track clicks using something like:
$("input:checkbox").change(function(){
$("input:checked").each(function(i){
//object/array manipulation?
});
});
I had it working okay using the index of the element but realized that wouldn't work since it would keep track of the order in which the checkboxes were checked in the first place.
Even a point in the right direction would be amazing. I realize I haven't given much to go on.
Thanks,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很好的挑战。您可以这样做:
如您所见,它“优先”将首先检查的复选框变黑。这不是经过测试的代码,可能有错误,但我希望它能指导您找到最终的解决方案
Nice challenge. You could do this:
As you can see, it has a 'preference' to blacken checkboxes that have been checked first. This is not tested code, and may have errors, but i hope it guides you to your final solution
您可以轻松计算选中的复选框的数量:
...其中
maxSlotsAvailable
是可用插槽的数量。上面的内容适用于页面上的所有复选框,但您可以通过选择器轻松地将其范围限定到特定容器。上面的内容允许这样的想法:即使所有插槽都已填满,用户也可以继续选中复选框;如果您要阻止这种情况,则可以删除
if
的 true 子句的第二行。 (我可能会让他们能够随意选中和取消选中复选框,只是用一个单独的指示器来显示他们选中的复选框太多,并进行预提交测试以确保没有太多。我认为听起来这也是你正在做的事情。)You can readily count the number of checked checkboxes:
...where
maxSlotsAvailable
is the number of available slots. The above works on all checkboxes on the page, but you can readily scope that to a particular container via the selectors.The above allows for the idea that the user can keep checking checkboxes even when all the slots are filled; if you're preventing that then you can remove the second line of the true clause of the
if
. (I'd probably leave them able to check and uncheck boxes at will, just with a separate indicator that shows that they have too many checked, and a pre-submit test to be sure there aren't too many. I think from the sound of it that's what you're doing, too.)