如何在 Jquery 中设置两个互斥的复选框?
$(document).ready(function() {
$("input:txtAge1").click(function(event) {
if ($(txtAge1).attr("checked") == true) {
$(txtAge2).attr("checked", "unchecked");
$(txtAge2).attr("checked") == false)
}
if ($(txtAge2).attr("checked") == true) {
$(txtAge1).attr("checked", "unchecked");
$(txtAge1).attr("checked") == false)
}
});
});
<input type="checkbox" id="txtAge1" name="option1" value=""/>21<br>
<input type="checkbox" id="txtAge2" name="option2" value=""/>55<br>
我正在尝试选择一个复选框或另一个。因此,如果未选中其中一个框,则要么不允许,要么强制 要选中其他框...换句话说,强制执行其中之一,但绝不允许 “未定义”条件
$(document).ready(function() {
$("input:txtAge1").click(function(event) {
if ($(txtAge1).attr("checked") == true) {
$(txtAge2).attr("checked", "unchecked");
$(txtAge2).attr("checked") == false)
}
if ($(txtAge2).attr("checked") == true) {
$(txtAge1).attr("checked", "unchecked");
$(txtAge1).attr("checked") == false)
}
});
});
<input type="checkbox" id="txtAge1" name="option1" value=""/>21<br>
<input type="checkbox" id="txtAge2" name="option2" value=""/>55<br>
I am trying to select either one checkbox or the other. So if one box is UNchecked, it should either be not allowed or force the
other box to BE checked ...in other words, enforce either one or the other but never allow
a "undefined" condition
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许我把这个问题简化了一点,但为什么不尝试使用单选按钮呢?
您可以将其中一个设置为
select
,以避免用户提交空值。更新:由于您的客户需要复选框,因此这里有一个 jQuery 解决方案:
这就是 jQuery 代码。您应该像这样设置输入框:
这应该可行,但未经测试。我可能错过了什么。
Maybe I'm dumbing down the issue a bit, but why not try using radio buttons?
You can set one to be
selected
to avoid the user submitting an empty value.Update: Since your customer wants checkboxes, here's a solution in jQuery:
That's the jQuery code. You should set your input boxes up like this:
That should work, but it's untested. I might've missed something.
一种解决方案是添加两个单击事件,每个复选框一个。当其中一个被单击时,另一个将被取消单击。
One solution is to add two click events, one for each checkbox. When one is clicked, the other is unclicked.
我最近遇到了这个问题,除了我需要复选框而不是单选按钮,因为需要取消选中这两个选项。我用这样的东西解决了它(适应OP的代码):
可能不是那么漂亮,但它有效。
I ran into this issue recently, except I needed checkboxes instead of radio buttons as having both options unchecked was a requirement. I resolved it with something like this (adapted to the OP's code):
Might not be that pretty, but it works.
我还想指出这个优秀的链接 http:// rndnext.blogspot.com/2009/08/mutually-exclusive-html-select-elements.html 此处。
但需要注意的是,我用它来互斥 div 内两个动态生成的选择列表。 。由于要操作的内容在页面加载时不可用,因此它无法按预期工作。以下解决方案位于 jQuery - 选择动态创建的 div 有助于解决该问题。
I also wanted to note the excellent link http://rndnext.blogspot.com/2009/08/mutually-exclusive-html-select-elements.html here.
One caution though, I used it to mutex two dynamically generated select lists inside a div. . Since the content to be manipulated is not available at page load, it was not working as expected. Following solutions at jQuery - selecting dynamically created divs helped resolve the issue.
我会使用这个功能。允许互斥并允许取消选中:
I would use this function. Allows mutual exclusion and allows uncheking: