如何撤消最后选中的复选框?
您好,我创建了一个 JavaScript 函数来检查所选模块的数量是否大于给定值。 因此,每次调用复选框时都会调用该函数,该函数会遍历所有复选框并计算总数以查看是否更大。 但问题是当用户选中复选框时,如果总学分大于该值,我想将复选框设置为checked=false。 但我不知道要撤消哪个复选框。 JavaScript中有撤消最后点击的功能吗?
Hi I created a javascript function to check if the number of modules selected is greater than a given value. So each time a checkbox is called the function is called, and the function goes through all the checkboxes and calculates the total to see if it's greater. But the problem is when the user checks the checkbox and if the total credits is greater than the value, I want to set the checkbox as checked=false. But I don't which checkbox to undo. Is there any undo last click function in javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
并非如此,但您可以通过保存最后单击的框来轻松伪造它。 这段代码可能无法逐字运行,但你明白了:
Not really, but you could fake it easily enough by saving the last box clicked. This code may not work verbatim, but you get the idea:
使用 jQuery,你可以做类似的事情:
如果你以前从未使用过 jQuery,这肯定是一个痛苦你的眼睛; 但正如您所看到的,它非常强大,您的问题可以用几行代码解决。 我建议您学习并尝试一下;)
Using jQuery, you could do something like:
If you've never used jQuery before, this surely is like a pain for your eyes; but as you can see, it's very powerful and your problem can be solved in few lines. I'd recommend you learning it and giving it a try ;)
我知道这不是您正在寻找的答案,但是当达到最大检查次数时禁用所有未选中的复选框不是更好的用户体验吗?
I know this isn't the answer you were looking for, but wouldn't it be a better user experience to disable all unchecked checkboxes when the maximum number of checks has been reached?
我认为不存在,但您始终可以保存对最后一个复选框的引用,因此当您必须取消选中它时,您就可以找到它。
I dont think there is, but you can always save a reference to the last checked box, so when you have to un-check it, you have it right there.
我建议测试是否已检查最大对象数,如果为 true,则以相同的方法删除所有检查,或者将某个隐藏文本框的值设置为上次检查的复选框的 id,以便您可以再次引用它。
I suggest either testing if the max number of objects has been checked and if true remove the check all in the same method or you set the value to some hidden text box to the id of your last checked check box so that you can reference it again.
这是一个最小但功能齐全且无 jQuery 的解决方案:
Here's a minimal, but fully functional and jQuery-less solution: