三个复选框,必须选中一个才能启用提交按钮,需要 Jquery 吗?
我有 1-3 个复选框,默认情况下它们全部被禁用。为了使提交按钮处于活动状态,必须至少选中复选框。有人可以帮我用 jquery 片段来实现这一目标吗?我的标记如下所示,该网站使用 jquery 1.42。请并谢谢你!
<form action="/cart/add" method="post" id="pform">
<h3 class="goudy">Make your selection:</h3>
<ul id="variants">
<li>
<input type="checkbox" name="id[]" value="39601622" id="radio_39601622" style="vertical-align: middle;" class="required" />
<label for="radio_39601622[]">$38.00 - Original Antique Photo</label>
</li>
<li>
<input type="checkbox" name="id[]" value="39601632" id="radio_39601632" style="vertical-align: middle;" class="required" />
<label for="radio_39601632[]">$8.99 - SCAN</label>
</li>
<li>
<input type="checkbox" name="id" value="39777962" id="radio_39777962" style="vertical-align: middle;" class="required" />
<label for="radio_39777962">$2.99 - Rigid Sleeve</label>
</li>
</ul>
<div class="buttons clearfix">
<input type="image" src="../images/add-to-cart.png" name="add" value="Add to Cart" id="add" class="send-to-cart" />
</div>
</form>
I have between 1-three checkboxes and by default they are all disabled. In order for the submit button to be active ove checkbox minumum must be selected. Can someone help me with a jquery snippet to achieve this? My markup looks like this and the site used jquery 1.42. Please and thankyou!
<form action="/cart/add" method="post" id="pform">
<h3 class="goudy">Make your selection:</h3>
<ul id="variants">
<li>
<input type="checkbox" name="id[]" value="39601622" id="radio_39601622" style="vertical-align: middle;" class="required" />
<label for="radio_39601622[]">$38.00 - Original Antique Photo</label>
</li>
<li>
<input type="checkbox" name="id[]" value="39601632" id="radio_39601632" style="vertical-align: middle;" class="required" />
<label for="radio_39601632[]">$8.99 - SCAN</label>
</li>
<li>
<input type="checkbox" name="id" value="39777962" id="radio_39777962" style="vertical-align: middle;" class="required" />
<label for="radio_39777962">$2.99 - Rigid Sleeve</label>
</li>
</ul>
<div class="buttons clearfix">
<input type="image" src="../images/add-to-cart.png" name="add" value="Add to Cart" id="add" class="send-to-cart" />
</div>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果没有检查任何内容,另一种禁用表单按钮的方法!
Another way with disabled form button if nothing checked!
假设提交按钮一开始就被禁用(经过测试):
Assuming the submit button is disabled to begin with (tested):
另一种方式:
Another way:
试试这个:
Try this:
我需要同样的功能,但在阅读了 aSeptik 的解决方案后,我很怀疑,因为它似乎在选中复选框时启用该按钮,并在未选中复选框时禁用该按钮。我对代码的阅读表明,只要您仅通过选中和取消选中一个复选框来测试它,该解决方案似乎就可以工作 - 尝试选中一个复选框,然后再选中第二个,然后取消选中其中一个,只留下一项被选中——即使您的复选框之一被选中,您也会看到您的按钮被禁用。我测试并确认确实是这样。
I needed this same functionality, but after reading aSeptik's solution I was suspicious, since it seems to enable the button any time a checkbox gets checked and disabled the button any time a checkbox gets unchecked. My reading of the code suggested that the solution would appear to work as long as you only test it by checking and unchecking one checkbox -- try checking one checkbox, then a second, then unchecking one of them, leaving only one checked -- you will see your button disabled even though one of your checkboxes is checked. I tested and confirmed that this is the case.